Skip to content

Compiled class

Structure of the generated class#

PHP classes are compiled into corresponding CLR types. The sample below depicts a PHP class and the resulting CLR type decompiled into the C# language.

class X {
  // ...
[PhpType("X", "index.php")]
public class X {
  // ...


The PHP class constructor is separated into three methods as follows:

  • [PhpFieldsOnlyCtor] protected .ctor(Context ctx)
    is a minimalistic constructor that initializes the instance without invoking a PHP constructor. This is used when a PHP class inherits another PHP class; in such cases, the base PHP constructor must not be called.
  • .ctor(Context ctx [, ... ctorparams])
    is used by C# programs and for the purposes of constructing a new instance of the object. .ctor() internally constructs the object, initializes fields and calls the proper PHP constructor if available.
  • __construct([ctorparams])
    is an optional PHP constructor that might be called explicitly or as a part of .ctor.

In addition, the compiled PHP class contains .cctor() (static constructor) that initializes internal runtime descriptors and call sites.

Additional class members#

The class also contains the following fields used by both the compiler and runtime.

  • protected Context <ctx>
    holds the reference to the owning Context instance.
  • internal PhpArray <runtimeFields>
    holds dynamic class fields; is null when empty.
  • IPhpCallable.Invoke(Context, PhpValue[])
    implementation of the IPhpCallable interface in case the class implements the magic __invoke function.
  • class __statics { ... }
    nested container for actual instances of PHP static fields. This is used by the runtime to manage different values of static fields for each Context (typically an HTTP Request).


The declared class methods are compiled as corresponding CLR methods. The provided PHP type hints are respected and translated to the corresponding .NET type. Whenever the PHP type hint is missing, PhpValue type is used instead.

Optional parameters (those with an initial value) are translated to corresponding CLR (.NET) initial value metadata for those types that allow this. In case the initial value is an expression or anything not allowed as .NET [Optional], a function overload is automatically created.


The type is annotated with [PhpTypeAttribute]. The attribute contains a fully qualified PHP class name and relative file path where it was declared.