Think C#, make PHP

MrCompiler

Operator overloading simulation for PHP.

PHP doesn’t support operator overloading. Cs2php is capable of simulating this feature while converting C# code to PHP.
Assume simple C# class

Class represents complex number. Some operators like multiplication has been defined in it. Following code represents ComplexNumber on PHP side.

Operator methods has been converted exactly like normal static methods. Method name was taken from C# (like op_Multiply) or from ScriptNameAttribute (like minus method).

Let’s take a look at operators usage.

And the same code in PHP

As you can see implicit conversion of a real number to a complex number was converted to a static method ComplexNumber::op_Implicit call. Moreover the addition and multiplication have been converted in similar way.

Of course CS2PHP can convert more complicated code, like this:

We can define more than one implementation for each operator. For example we can define two independent multiplication operators:

We have to decorate at least one definition with ScriptNameAttribute in order to specify different PHP names for both methods.

And now we can test this solution using following code:

How to create dynamic images with C#?

 

Overview

Lang.Php.Graph namespace contains Image class that represents PHP resource related to GD image that can be obtained using functions like imagecreatetruecolor. Image class is also container for image related methods.

On PHP side we have a number of functions with name starting from image prefix accepting GD image resource as first argument like this.

On C# side we can use instance methods usually with shorter names and with reduced argument list. For example imagecreatetruecolor equivalent is defined as:

And we can use this method like this:

Color, Font and other

Lets take a look at imagestringfunction defined as

This function accepts font and color as integer value, so it is easy to make mistake by putting any integer value that was not intended to be font or color.

To avoid this ambiguity Lang.Php.Graph namespace contains classes like Color or Font. By using this classes we can produce more reliable code even if on PHP side they are nothing more than integers. That’s why DrawString (imagestring equivalent) is defined as

How can we use Color class?
ColorAllocate method returns Color while its PHP prototype returns int (if success) or FALSE when action fails.
Meticulous approach to programming requires error checking. Color class contains static function IsValid, defined as

And its typical usage is

which is translated into

Example

Code below is a part of example available on GitHub. Class DynamicImage can generate images like this

example04image

Using PHP data structures in C#



Php is full of functions that returns various data structures sometimes quite complicated and not easy to remember. One of most weird structures is return value of getimagesize. It is an array containing values indexed by both integer and string indices.

Assume output of some example code:

We can expect output like this:

array (size=7)
0 => int 3456
1 => int 2592
2 => int 2
3 => string ‚width=”3456″ height=”2592″‚ (length=26)
‚bits’ => int 8
‚channels’ => int 3
‚mime’ => string ‚image/jpeg’ (length=10)

Following PHP manual we know that index 0 and 1 contains respectively the width and the height of the image. Index 2 is value indicating the type of the image and so on.

Let’s start dreaming about more comfortable way to access image properties. Lang.Php.Graph namespace contains class ImageInfo.

Note that class is decorated with AsArrayAttribute so it will not been converted to normal PHP class. Array will be used instead.

Fields are decorated with ScriptNameAttribute so every C# expression using field of ImageInfo class is converted into PHP array access expression.
For example:

Is converted to

What about image type? On PHP side we have integer value. Value 2 (or IMAGETYPE_JPEG constant) indicates Jpeg.
On C# side we have Lang.Php.Graph.ImageTypes enum. Source:

can be translated to:

Installed with WPInstant