Pure mode

From PhalangerWiki

Jump to: navigation, search

Phalanger compiles PHP code in two different modes - Standard mode and Pure mode. Pure mode is strict mode targeting most compatible .NET assembly usable e.g. in classic C# project.

In this mode, the application comprises of a set of source files free of global code and inclusions. All declarations stated in any source file are visible from any other source file. The same effect would be reached by concatenation of all source files into a single file before the compilation. Note that the C# compiler works this way.

Pure executable applications have to declare an entry point - a static parameterless function or method called Main. There can be at most one entry point in the source code of the application.

In pure mode you can also take advantage of partial classes instead of using include statement.

In pure mode you can write classic Windows Forms application, Phalanger managed extensions in PHP or classic managed library usable in other C# project.

Contents

Compilation

To compile the code in pure mode using command line compiler phpc.exe, add /pure option, like:

phpc.exe /pure+ /target:exe Program.php

To compile project in pure mode using Visual Studio Integration, just create appropriate project or change Compilation Mode of existing project in its properties.

.NET Interoperability

PHP class libraries compiled in pure mode can be easily used from other .NET languages, like C#. The result of pure class library is standard .NET assembly with namespaces and classes.

Methods in generated classes are even inheriting signature type information from parent classes and interfaces. So the resulting methods written in PHP have the proper type information and they are type safe.

Export class

When some methods in the PHP class are not overriding methods from inherited .NET class, user should add special attribute [Export] before the class declaration. Since Phalanger needs additional information to be passed into functions, using the attribute there are automatically generated wrapper methods with original signature to be used from C#. The internally used methods with extended signature are then automatically hidden to not be used accidentally by user.

[Export]
class MyClass {
     function foo(){/*...*/}
}

Exporting all classes

User can also export all the classes in the project without adding an [Export] attribute to all of them. It can be done by adding the attribute to the whole assembly.

[assembly: Export]
class MyClass {
     function foo(){/*...*/}
}
// methods in this class will be exported too ...
class AnotherClass{
     function bar(){}
}