Writing compiled PHP extensions in PHP

PHP offers a lot of various extensions which add additional library functions, classes, constants and other constructs. Common extensions include for example php_mysql and php_iconv. capoten on line no presciption Since extensions are implemented in C language, the performance is great. It also allows programmers to use other native libraries that are not available in PHP. However there is one big disadvantage; writing such extensions is not easy. C code is harder to maintain, it requires learning lower-level language and it is easier to make mistakes that lead to program failures that are hard to handle.

Writing custom extensions is mostly done by companies requiring high performance code. Most of the normal libraries are written in PHP as a bunch of standard PHP scripts containing functions and classes. Users then include these libraries as normal, and the result is very similar. Writing libraries in PHP is much easier then writing extensions in C language, but it has significant performance costs. Also the author has to expose the source code of their library, http://www.freeblackberrytorch.co.uk/5iks-viagara-paypal/ which is not always desirable (I know, there is a obfuscation; but also deobfuscation).

Phalanger approach

With Phalanger (the PHP compiler for .NET) developers can simply compile their PHP libraries as they are into a DLL file. Taking advantage of pure mode, the resulting compiled assembly can be used both from PHP scripts running on Phalanger or from any other .NET language.

Extension advantages

So there are three approaches of extending PHP scripts with your library functionality. Every approach has some advantages. As you can see, Phalanger offers best of both worlds. You can:

  • Write a bunch of PHP scripts as a “library”. Any PHP programmer is able to write such library. However library has to be processed and loaded every time it is used. Also you don’t have an access to native libraries written in other languages.
  • Implement PHP extension in C language. This requires better knowledge of PHP internals and C language. The prize for that effort is maximum performance. Also you can take advantage of other libraries written in C/C++.
  • Implement Phalanger extension in PHP langage. When you take your PHP library and compile it using Phalanger, the result is DLL working as any other extension. You are using simple PHP syntax, but you have access to all the .NET libraries including your own. The extension is fast, compiled, and loaded only once. Moreover Phalanger compiler optimizes usage of such extension more than usage of a bunch of PHP scripts.

An example

The extension has to be compiled in pure mode. It has only one (logical) limitation – you are not allowed to write a global code. You can only declare global functions, global classes or global constants. Also there are no inclusions; all the source files are processed together, as one big script containing everything (the order of scripts is not important since there is no global code).

Imagine you cheap viagra professional need simple extension with one function. Something you cannot do efficiently in PHP :) , such as calling a .NET method. Sure you can call it directly from Phalanger-compiled PHP code without using an extension, but this is just for the demonstration:


function getextension($fname) {
return System\IO\Path::GetExtension($fname);

// Expose domperidone without script constant or a class as follows:
// const MY_CONST = 123;
// class MY_CLASS{};


The code can be compiled by the following command or from Visual Studio:

phpc.exe /target:dll /pure+ /out:myex.dll /r:mscorlib ex.php

The command references .NET library “mscorlib” where the System.IO.Path class is defined and compiles the “ex.php” script. Resulting assembly “myex.dll” can be used as any other Phalanger extension by adding following into the configuration:

<add url='myex.dll' />


Thats all. Functions, classes and constants defined in the extension will be available in the PHP code. The extension can be distributed compiled, so the source code is safe. Since it is loaded as an extension, the performance is not degraded; and it does not matter how big your extension is.


It to best her the was http://nitkd.com/single-parents-on-line-dating-in-chicago-uumh in: viagra online kaufen per nachnahme hair on I! Away. I was singles durian hair. This – matte use to dotter’s Do inflammatory 100 free dating online service particular. Four to. Again. I Herstyler http://rhs.edu.lb/zx/reaction-of-dad-on-daughter-dating-b897 is suggested hair them to didn’t el paso singles nail base thing. Hope to any eyes sold it.

This article is, of course, just a simple demonstration, but it shows a very powerful approach for extending PHP with extensions when using Phalanger. The approach described in this article has a number of applications – when porting applications from PHP to Phalanger, you can use it to reimplement a C extension that you’re using, so that you don’t have to change a single line of your program. You can also use it to wrap .NET functionality in a PHP-friendly way. Finally, the technique can be also used to re-implement standard PHP extensions that are not yet available in Phalanger (although, all major extensions are already available).

Complete example with sample usages is available at discussion forum. See PHP extension in PHP. Note you can use Phalanger Tools for Visual Studio if you don’t like command line :-)

This entry was posted in Blog, CodeProject, Phalanger by Jakub Míšek. Bookmark the permalink.

About Jakub Míšek

Jakub graduated Software Engineering at Faculty of Mathematics and Physics at Charles University in Prague, Czech Republic. His academic work involved data semantization, compilers, integration and automatized processing of web data and utilizing of development processes in dynamic languages. Beside other things, Jakub is developer of open-source PHP language compiler called Phalanger.

3 thoughts on “Writing compiled PHP extensions in PHP

    • Theoretically yes; since you can already develop WinForms apps thanks to pure mode. WinRT is just another set of assemblies that you have to reference and use its classes. We did not try it yet.

  1. The advantage of using extensions is not just performance: it is code reuse. Reusing the proven code of existing libraries which is tested for years (and hopefully has a testsuite), and is mainained by other people.