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 {
  function __construct() {
    // ...
[PhpType("X", "index.php", 0)]
public class X {

  // Reference to Context associated with the current instance.
  // Context is needed for most of PHP functions and PHP program lifecycle.
  protected readonly Context <ctx>;

  // Lazily instantiated array with dynamically added fields.
  internal PhpArray <runtime_fields>;

  // Constructor used internally to initialize the class without calling the `__construct` function.
  [CompilerGenerated, PhpFieldsOnlyCtor]
  protected internal X(Context <ctx>, DummyFieldsOnlyCtor _) : base() {
    this.<ctx> = <ctx>;
  // Regular class constructor.
  public X(Context <ctx>) : this(<ctx>) {
  // Context-less constructor to be used from other .NET languages.
  // Context is obtained or created for the current `ExecutionContext`.
  [CompilerGenerated, PhpHidden]
  public X() : this(ContextExtensions.CurrentContext) {
  // Compiled `__construct` function.
  public PhpValue __construct() {
    // ...


The PHP class constructor is separated into several .ctor overloads as follows:

  • [PhpFieldsOnlyCtor] protected internal .ctor(Context, QueryValue``1)
    is an internally used constructor that initializes the instance without invoking the __construct() method. This is used when inheriting another PHP class, for object unserialization and when cloning. Note, the special QueryValue``1 parameter is here just to distinguish this internal .ctor and the regular .ctor.
  • .ctor(Context[, ... ctorparams])
    is used 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.
  • .ctor([, ... ctorparams])
    is used by C# programs to create a new instance of the object. It is a wrapper of the .ctor above that does not require the instance of Context.
  • __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 by default.
  • 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 annotates the type with following properties: - ExplicitTypeName (string) - fully qualified PHP type name as it appears in PHP context. - FileName (string) - relative file path where the type was declared. - Autoload (byte) - flag indicating how was the type set for autoloading. See composer.json for enabling the autoload. The values of the flag are: - 0: the type was not marked as autoloadable. - 1: the type is set to be autoloaded, but containing file or base types have side effects. - 2: the type can be safely autoloaded without side effects.


The attribute annotates types of type trait. See compiled trait for more details.


Every PHP class is bound to a Context. Internally every class is instantiated using a reference to an existing Context object. (This object is used for operations like echo and maintaining PHP options and request state).

It is possible to omit the Context instance; then keep in mind a new Context is created within the current ExecutionContext. In case of ASP.NET Core application, the Context is bound to the current HttpContext expecting the HttpContextAccessor service is registered.