Libraries Architecture

The library is represented by a common .NET class library (.dll file). Once the library is referenced within the compilation process, the contained symbols (types, functions and constants) become accessible to the compiled program. Once the library is referenced, it has to be distributed together with the compiled program. There are two types of libraries:

Standard .NET class library - its public types become available to the runtime and can be used as PHP classes.

Extension library is a .NET class library containing an assembly attribute [assembly: Pchp.Core.PhpExtension]. This tells the compiler to treat the contained declarations differently.

Assembly attribute that tells the compiler to treat the assembly as an extension library.

[assembly: Pchp.Core.PhpExtension]

internal and private symbols won't be visible to the compiled script.

public class or public interface with [PhpType] attribute becomes available to the compiled script as a compatible PHP class or interface even within the same namespace.

// Example:[PhpType]publicclassArrayIterator:IEnumerable,ArrayAccess,Countable{// declaration of a PHP method in C#publicvirtualvoidfoo(PhpArrayarray=null){...}// declaration of a class constant in C#publicconstintA_CLASS_CONSTANT=123;// declaration of application-wide static fieldpublicstaticintStaticField;// declaration of a container containing context-static fields and constants// instance of such fields is created for every request separately and is thread-safepublicclass_statics{publicreadonlyintContextConstant=789;publicstringContextStaticField="abc";}}

public static class represents a container containing the declaration of global PHP functions and constants. The container is ignored if it is annotated with the [PhpHidden] attribute.

// Example:publicstaticclassMyFunctions{// declaration of a global PHP function 'mystrlen()'publicstaticintmystrlen(stringstr){return(str!=null)?str.Length:-1;}// declaration of a global PHP function 'myecho()' that requires a reference to current Pchp.Core.Context representing PHP runtime.publicstaticvoidmyecho(Contextctx,intvalue){ctx.Echo(value);}// declaration of a global PHP constant 'MYCONST'publicconstintMYCONST=456;// declaration of a global constant that is initialized in run timepublicstaticreadonlyintMYCONST2=Environment.OS.Version.Major;// declaration of a global constant as a C# property, evaluated every time the constant gets usedpublicstaticobjectMYCONST3=>newstdClass();}