Phalanger http://www.php-compiler.net The PHP language compiler for .NET Framework Wed, 23 Apr 2014 18:39:28 +0000 en hourly 1 http://wordpress.org/?v=3.3.2 Phalanger 4.0 http://www.php-compiler.net/blog/2014/phalanger-4-0 http://www.php-compiler.net/blog/2014/phalanger-4-0#comments Mon, 07 Apr 2014 14:25:55 +0000 Jakub Míšek http://www.php-compiler.net/?p=969 Continue reading ]]> After the long break we are happy to announce progress on Phalanger and the new version 4.0. There are some major changes in functionality and planned changes in Phalanger API. Following blog post should summarize them and describe Phalanger 4.0 advantages and improvements.

In previous releases most of the issues were caused by complexity of the compilation process and the need of low level configuration of ASP.NET and Phalanger to achieve working Phalanger application. With Phalanger it was possible e.g. to build WinForms, ASP.NET Forms, console apps, class libraries; in two compilation modes with different parser settings.

Phalanger 4.0 will simplify the process of compiling and running PHP applications. Not used features will be deprecated. New PHP 5.5 features may be added and the compilation process will be improved to achieve better performance in run-time.

Major changes

  • Support for native PHP 4 extensions was removed. It is no longer possible to wrap native C extension for PHP 4 into a .NET assembly and use it is an extension for Phalanger. PHP 4 extensions caused lagging and memory leaks. Its configuration and use is confusing, with the need to run in 32 bit mode.
  • Precompiled Web Sites are automatically loaded from /Bin folder. It is possible to compile a bunch of PHP scripts into a .NET assembly. It is no longer needed to name such assembly “WebPages.dll” and it is no longer needed to list such assemblies within Web.Config file.
  • Distributed with Phalanger Tools for Visual Studio. Since debugging, building new Web Projects and configuring go together, Phalanger will be distributed with up-to-date add-in for Visual Studio. This allows users to update Phalanger through Visual Studio extensions manager, keep both products in sync and work better with their Phalanger projects.
  • Up-to-date Tools for Visual Studio. New Phalanger Tools are built on top of PHP Tools for Visual Studio. Thanks to this integration Phalanger Tools will take advantage of continuously improved features of PHP Tools. Based on huge users feedback, the integration gets faster and more intuitive.
  • New API for parsing and compiling. Phalanger is about running PHP projects under .NET, integrating with .NET and improving performance of PHP code. Integrating with .NET projects is very important and unique feature. This release of Phalanger introduces new API that will be subsequently improved and documented. This allows developers to do frequent tasks with PHP code from .NET languages like C# or VB.

Future changes

  • Improved performance. New API allows to bring better code analysis and to produce much faster compiled assemblies. Also thanks to the missing support for native PHP 4 extensions, it will be possible to improve internals of Phalanger, including application startup and more.
  • Built-in obfuscation. Phalanger is being frequently used as a PHP obfuscation tool. With this release of Phalanger, Phalanger Tools will come with Obfuscate option to protect compiled code, string literals, functions body and names of function arguments and name.
  • Easier development of extensions. Since this release, it will be easier to implement missing extensions for Phalanger in .NET or PHP itself. This will be achieved by new project templates for Visual Studio and simplified interface for a Phalanger extension.

The list above gives quick look on major changes and planned improvements. Any new progress will be announced on Facebook, Twitter or here. The release will be available soon at CodePlex or newly at Visual Studio Gallery.

Resources

]]>
http://www.php-compiler.net/blog/2014/phalanger-4-0/feed 8
Phalanger 3.0 updates for March 2013 http://www.php-compiler.net/blog/2013/phalanger-3-0-updates-for-march-2013 http://www.php-compiler.net/blog/2013/phalanger-3-0-updates-for-march-2013#comments Wed, 06 Mar 2013 13:44:42 +0000 Jakub Míšek http://www.php-compiler.net/?p=957 Continue reading ]]> After several months of development, contributions from opensource community and collaboration with big commercial users, Phalanger is getting bigger. Today we’ve released package of Phalanger, containing many new extensions and latest integration for Visual Studio.

New goodies in Phalanger

Mainly Phalanger is getting more managed extensions.

Biggest advantage of Phalanger is its platform; Phalanger is built completely on .NET, using safe code. No native unsafe C/C++ code is being used, and so Phalanger does not suffer from common bugs like memory leaks, buffer overruns and others. However to achieve compatibility with existing PHP applications, it needs to support a lot of PHP extensions. Until now the only way was to use mechanisms of remoting and DLL hijacking, so old native PHP4 extensions might be used in otherwise clean .NET environment.

Since this release, Phalanger contains more extensions implemented directly in .NET, so the whole process can run managed, 64bit (AnyCPU if you want) fully taking advantage of .NET framework. See following section for new extensions.

Beside new extensions, Phalanger gets more PDO and SPL functions/classes and more PHP 5.4 and 5.5 (short array syntax, function array dereferencing, indirect static method call, binary number format, new callback options, boolval() function).

Complete package for development

Phalanger Setup package contains everything developer may need for running common PHP web or application under .NET with all its advantages.

Phalanger supports more extensions now, so it allows many more popular PHP projects running natively on .NET, without any unsafe native code.

  • Class Library (PhpNetClasslibrary.dll) is basic part of Phalanger containing basic set of functionality (standard,Core,session,ctype,tokenizer,date,pcre,ereg,json,hash,SPL,filter).
  • cURL (new) – for most common tasks, Phalanger now comes with cURL extension suporting HTTP/HTTPS protocols. Community is now free to extends its functionality  as they need.
  • GD2, exif and image (new) are well known PHP extensions allowing to read/manipulate with images.
  • Iconv (new) for string encoding conversions built on .NET Encoding.
  • MSSQL is Microsoft SQL extension using SqlConnection internally to increase performance. It also ensures compatibility with latest Microsoft SQL servers.
  • PDO (new) is an abstraction over PHP database connections. Support for PDO was added, containing several DB drivers like SQLite or MySQL. Developers are free to extend PDO support with additional DB drivers now.
  • SoapClient (new) is managed reimplementation of PHP SOAP taking advantage of .NET built-in SOAP support.
  • SQLite (new) is another DB extension for Phalanger.
  • MySQL extension for Phalanger takes advantage of latest managed Oracle/.NET connector. This makes DB operations faster and safer, allowing to configure additional options and security options in standard .NET-way.
  • XML (new) extension is now contained in Phalanger too. Must-have extensions commonly used for its utf8 functions.
  • XMLDom extension contains support for PHP SimpleXML, dom, xsl and libxml extensions. Its feature set was extended by libxml functions and improved HTML parsing functions. The extension takes advantage of .NET XML built-in support which  offers great performance and security.
  • Zip (new) extension was added thanks to community contributions. Anyway still needs some work to be finished.
  • Zlib (new) extension is essential part of many PHP projects, mainly because of its gzip compression support. A part of Phalanger now.

To simplify development, Phalanger comes with its integration for Visual Studio. This allows to create .NET desktop applications written in PHP, or a WebSite running on ASP.NET and compiled seamlessly from PHP. See Visual Studio Gallery for more information. Note the integration requires Phalanger to be installed using its Setup.

Commercial Services

Phalanger is still a free software. Companies and single developers may consider visiting commercial services page for information about migration assistance, technical support or feature requests.

As a part of commercial services you can also obtain additional memcached extension for Phalanger, which was not open-sourced currently.

Mono runtime

Don’t forget about Mono; thanks to cooperation with Mono core developers, Phalanger runs on Mono fluently. This helped both Phalanger and Mono to improve their stability and reliability.

Future plans

Phalanger is growing every day. Currently there are big plans about another increase of compiled apps performance. More smart compiler stuff will be introduced, and more compiler configuration options will be designed.

Of course every day task is to keep up with new PHP features; Anyway great thing about Phalanger is it takes advantage of .NET, ASP.NET and IIS – most of PHP issues are simply not possible on these platforms which are continuously developed by Microsoft. In future Phalanger will get missing PHP 5.4 sweets like traits and closures rebinding.

Stay tuned on twitter or facebook group for more details. Thanks!

]]>
http://www.php-compiler.net/blog/2013/phalanger-3-0-updates-for-march-2013/feed 0
Announcing WP.NET http://www.php-compiler.net/blog/2012/announcing-wp-net http://www.php-compiler.net/blog/2012/announcing-wp-net#comments Wed, 23 May 2012 16:08:50 +0000 Miloslav Beňo http://www.php-compiler.net/?p=925 Continue reading ]]> Phalanger was capable of compiling WordPress for quite a long time, but this support was always targeted at developer audience. Experienced developers could compile WordPress and run it on their servers. However, we noticed that the combination is interesting not only to developers, but also to end users. Some of WordPress users want to run it on .NET or on Mono. Mainly for performance reasons, but some of them also because they already have other stuff running on .NET. The problem was it is quite a tough job to install Phalanger, configure, compile… don’t mentioning that on shared hosting it can be a problem to install anything.

So today we’re happy to announce WP.NET (wpdotnet.com) – A package of WordPress with embedded Phalanger. One package gives you WordPress for both .NET 4.0 and for Mono (2.10.8 and higher), with no installation hassles. If you have ASP.NET hosting it’s easy to deploy and when a new Phalanger is available, you can update it from administration section, just easily as WordPress update.

WP.NET is fully managed ASP.NET application. It does not contain any native code, so it’s possible to run it 64bit on Windows.  The same package also works on Linux using Mono with version greater than 2.10.8.

Main advanteges are that WP.NET is faster than WordPress running on normal PHP and it’s also extendable with .NET languages. Which makes it great CMS that anyone can extend and it doesn’t matter what language he chose to use. The tutorial on how it’s possible to extend it with C# is going to be published withtin few days.

Some of the PHP plugins or more advanced functionality can require some more PHP extensions that are not included in package as it’s released today. We are aware of it and there will be available package with most important managed extensions.

In the future we also plan to get WP.NET available via the Web Platform Installer.

You can find the package on its new home page wpdotnet.com and its sources on its GitHub repository https://github.com/DEVSENSE/WpDotNet. Feel free to check it or improve it ;-) .

]]>
http://www.php-compiler.net/blog/2012/announcing-wp-net/feed 18
Phalanger 3.0 updates for March 2012 http://www.php-compiler.net/blog/2012/phalanger-march-2012 http://www.php-compiler.net/blog/2012/phalanger-march-2012#comments Mon, 27 Feb 2012 16:43:39 +0000 Jakub Míšek http://www.php-compiler.net/?p=840 Continue reading ]]> On February 2012, Phalanger – the PHP compiler for .NET – got a lot of new features, fixes and optimizations. You can see the full list of changes (still growing) at phalanger.codeplex.com. The most noticeable change is the performance of accessing PHP arrays and overall performance compared to previous versions and standard PHP itself!

Performance

You can check full benchmarking results at Benchmarks page. Phalanger performs great even in comparison with latest release of PHP using op-code cache. Still, there is a lot of other possibilities of improving Phalanger (planned for future releases).

Compared to standard op-code cached implementation of PHP, running on Windows, Phalanger is able to serve significantly more pages per second. Since Phalanger compiles PHP code into .NET assemblies which are later compiled into native machine code using .NET Just-In-Time compiler, the generated code uses maximum from your actual server’s CPU.

Phalanger performs compile time analysis of the code. It allows to build smaller op-code that does exactly what it has to do without an undesirable overhead.

Note in addition to better performance; Phalanger gives you .NET interoperability, security, compile-time code verification and powerful development environment, where you can debug your PHP projects as you are used to from C#.

New features

Phalanger is being more and more compatible with actual PHP 5.4 “specification”. List of missing features is smaller every month. You can take a look at wiki.php-compiler.net/Compatibility to ensure, there is really a little things missing. Most of PHP applications work out of the box, needing a little or none modifications.

Some new features:

  • Improved implementation of PHP array which saves memory and CPU ticks.
  • “filter” extension partially implemented.
  • PHP compatible mail()ing.
  • Faster concatenation, arithmetic operations, unary operators, …
  • Working with .NET IDictionary and IList seamlessly as with PHP arrays.
  • Nice(r) debug view using Phalanger Tools for Visual Studio.
  • flock() available on Windows.

Also we are running new discussion forum at support.devsense.com (which is unmodified Phalanger-powered phpBB). Feel free to ask/list or answer any question you’d like to.

]]>
http://www.php-compiler.net/blog/2012/phalanger-march-2012/feed 10
Writing compiled PHP extensions in PHP http://www.php-compiler.net/blog/2012/php-extension-in-php http://www.php-compiler.net/blog/2012/php-extension-in-php#comments Tue, 07 Feb 2012 14:11:40 +0000 Jakub Míšek http://www.php-compiler.net/?p=813 Continue reading ]]> 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. 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, 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 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:

<?php

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

// Expose 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.

Summary

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 :-)

]]>
http://www.php-compiler.net/blog/2012/php-extension-in-php/feed 3
WordPress on .NET with SQL Server is Possimpible using Phalanger 3.0 http://www.php-compiler.net/blog/2012/wordpress-on-dotnet-with-sql-server-using-phalanger-3-0 http://www.php-compiler.net/blog/2012/wordpress-on-dotnet-with-sql-server-using-phalanger-3-0#comments Mon, 23 Jan 2012 14:05:36 +0000 Marc Mezzacca http://www.php-compiler.net/?p=776 Continue reading ]]> In a recent scenario I wanted to run WordPress as a subdirectory of a .NET application. I also wanted to avoid installing PHP and MySql on the Windows server. Impossible? Apparently not! (I’ll get to the word Possimpible a bit later).

I already have IIS and SQL Server setup, so I want to utilize those with WordPress. To do this I installed a pre-compiled version of WordPress, using Phalanger, as well as a simple WordPress plugin to avoid having to install MySql.  Here is what I did…

Contents

Download the Goods

First, you’ll need to grab a few things prior to starting.

The environment I worked on was IIS 7/7.5 with SQL Server 2008 Express and .NET 4.0 on Windows 2008. You should have a similar combination of software installed if you expect it to work properly after following this tutorial. I also used SQL Server Management Studio Express to make things a bit easier for creating the database. If you don’t have it, you can grab it here.

NOTE: This post is heavily based on Miloslav’s previous post for Installing WordPress on .NET 4.0, however I’m using SQL Server instead of MySql and a newer version of Phalanger.

Install Phalanger 3.0

Extract the contents of the download and run the Setup Wizard to install Phalanger by following the on-screen instructions. Should be simple enough!

NOTE: You can skip this step if you wish to do a standalone deployment or don’t have access to the server, as written in the Installation Free Phalanger article previously by Jakub. Be sure that you have the Microsoft Visual C++ 2010 SP1 x86 Redistributable installed or it will throw an error about php4ts.dll not being found!

Install WordPress

Extract WordPress to a directory of your choosing within IIS.

NOTE: I downloaded the IIS version of WordPress, although I’m not certain it matters.

In this tutorial I already have an ASP.NET 4.0 Web Application running in a directory (cfv2). I am going to install WordPress as a separate Web Application, but as a sub-directory that lives within this website. There are a couple ways to do this, but in this case I am installing it in a physical subfolder of the existing .NET web application. I created the directory at:

c:\inetpub\wwwroot\cfv2\wpblog\

NOTE: You can use wwwroot of IIS or any other virtual directory.

Next, set write and modify permissions for IIS_IUSRS on this folder. This is necessary since WordPress creates wp-config.php file during the installation. Also, WordPress needs the write permission to allow auto update feature, downloading themes, plugins, etc.

To set the permissions, open Command Prompt with the Administrator permissions and run the following command:

icacls c:\inetpub\wwwroot\cfv2\wpblog /grant BUILTIN\IIS_IUSRS:(OI)(CI)(M)

NOTE: If you don’t want to allow this you can create the configuration file manually during the installation. WordPress recognizes that it doesn’t have permissions to create the file and gives you content for this file. Then you can create the file manually.

Setup the Web Application

I’ve created the physical subdirectory (wpblog), but now I need to set it up as a Web Application in IIS. There are a couple steps here that may differ for you depending on your setup.

Create a 32-Bit Application Pool

For the open source version of Phalanger to work properly you’ll need an Application Pool that is 32-Bit enabled. So here I will create a new Application Pool that will run in 32-Bit Mode. In IIS go to the Application Pools area under your server. Right click and do “Add New Application Pool…” I called mine WordPress. Select .NET 4.0 or above and click OK.

clip_image002

Next, check the list of Application Pools that should appear on the right. Select the newly created one and right click and select “Advanced Settings” option. Change “Enable 32-Bit Applications” from False to True.

clip_image004

Create a Web Application

In IIS right click and select the “Add Application”, then set the Physical Path to the wpblog directory. I’m going to call it “blog” so eventually it will look like http://mysite/blog/

clip_image006

Next select the previously created Application Pool (called WordPress as mentioned in the previous step)

clip_image008

Add a Web.config File

Configure ASP.NET using Web.config.  The web.config file will reside in the root of the wordpress directory (in this case wpblog). Here is an example web.config file that uses Phalanger 3.0 references.

View the sample web.config file.

Alternatively, view the sample web.config file for standalone installations including URL rewrites for pretty permalinks.

More information about configurations can be found here in the wiki.

Setup your SQL Server database

Create a new empty SQL Server database for the WordPress stuff to reside. Optionally, you can skip this if you have already created one or are planning on using an existing database.

clip_image010

Be sure to setup a user with high enough privileges to create the tables.

clip_image012

Setup the WP Database Abstraction Plugin

Now we’ll need to follow the DB Abstraction plugin instructions.

Upload wp-db-abstraction.php and the wp-db-abstraction directory to wp-content/mu-plugins. This should be parallel to your regular plugins directory. If the mu-plugins directory does not exist, you must create it.

clip_image014

Move the db.php file from inside the wp-db-abstraction directory to wp-content/db.php

Setup and install the plugin by going your wordpress url (eg. http://localhost/blog/) with the subdirectory /wp-content/mu-plugins/wp-db-abstraction/setup-config.php appended to generate your wp-config.php file.

clip_image016

Follow the on-screen instructions from the point on…

That is it. Now just follow the normal WordPress setup and you’re home!

NOTE: After installation, see Miloslav’s article for information about setting up permalinks by modifying your web.config file.

Optional: Pre-Compile WordPress with Phalanger

Phalanger can compile the source code to a .NET assembly for improved performance. This is suggested!

NOTE: It is not necessary to precompile the web page; single scripts will be compiled during first requests subsequently. However it is recommended to precompile the application to make sure the application is error free and to lower response time during first request.

To do this, create a build.bat file in the wordpress directory with something similar to the following content:

@echo off
"C:\Program Files (x86)\Phalanger 3.0\Bin\phpc" /target:web /root:. /recurse:. /out:Bin /static+ /debug-

This batch file runs the Phalanger compiler to perform the pre-compilation. Detailed documentation for each of phpc option can be found here.

clip_image018

Error during Build? Here is a quick fix!

It will likely give you an error when you attempt to build (although this may be fixed in the future), so you have to make a slight modification to the WP Abstraction Plugin. I encountered an error with the Data Abstraction plugin at line 562 of the pdo.php:

clip_image020

It seems to choke on the “break;” line in the pdo.php since it is not in a loop. Modify it to “return;”, save the file, and then it should compile OK.

After it compiles successfully, the Bin folder should have a WebPages.dll file inside.  If you are doing a standalone install, you’ll need to drop all the necessary Phalanger DLLs in the Bin as well.

Concluding Thoughts

This is a real-world walk-through from notes as I set it up for my own purposes. There are numerous variations you can take along the way to set it up in your own environment and this post should only be used as a guideline. Definitely spend time testing it out in a local or sandbox environment before putting it into production.

I would highly suggest using the pre-compiled version versus non-compiled as it seems to offer some speed benefits. I tried both — the non-compiled version seemed to intermittently take long periods to load pages and sometimes would even timeout (load a blank page).

For high-level production environments, Devsense offers a Managed Extensions Pack for a fee, which provides increased speed, security and the ability to run in 64-Bit mode.

I’m a big advocate of using open-source .NET stuff, and also giving back. If you use the open-source version of Phalanger, please be sure to contribute by donating to the project.

That’s all! See, I told you WordPress on .NET with SQL Server is possible. In fact, it’s Possimpible (finally see the reference here). Still a skeptic? Check it out live at http://couponfollow.com/blog/ and be amazed! Well it needs some UI theme updates… and some content… but hey, it is working.

About the Guest Author

Marc Mezzacca is a .NET Programmer, entrepreneur, and founder of NextGen Shopping, a company dedicated to creating innovative shopping mashups. Marc’s latest venture is a social-media based coupon code website called CouponFollow that utilizes the Twitter API.

]]>
http://www.php-compiler.net/blog/2012/wordpress-on-dotnet-with-sql-server-using-phalanger-3-0/feed 16
Phalanger riding Mono http://www.php-compiler.net/blog/2012/phalanger-php-mono http://www.php-compiler.net/blog/2012/phalanger-php-mono#comments Tue, 17 Jan 2012 18:00:55 +0000 Jakub Míšek http://www.php-compiler.net/?p=637 Continue reading ]]> Phalanger is a complete reimplementation of PHP, written in the C# language. It was always being developed with the Mono platform in mind. This means you can compile and run PHP application on Linux web servers using Mono. Since Phalanger 3.0, this become more official, periodically tested and maintained.

Notes

Mono since 2.10.8 contains few fixes that allow running Phalanger powered applications. Mainly it fixes the recursive ReaderWriterLockSlim issue, which disallowed Phalanger in some special cases. If you encounter this issue, please update your Mono to version that has this fixed.

Installing Phalanger on Linux

Briefly, see configuration and add listed configuration options into your web.config file. Dependant Phalanger’s assemblies copy into Global Assembly Cache using “mono gacutil.exe -i” util. You will need PhpNetCore.dll, PhpNetClassLibrary.dll and required extensions (e.g. PhpNetMySql.dll, PhpNetSimpleXml.dll).

The rest of configuration is the same as for ASP.NET 4.0 web on Mono.

Too short?

This post is more an announcement than a tutorial of installing Phalanger on Mono. Phalanger installer for Linux will be published soon, so you don’t have to care about installing :-)

]]>
http://www.php-compiler.net/blog/2012/phalanger-php-mono/feed 5
.NET interoperability overview of Phalanger 3.0 http://www.php-compiler.net/blog/2012/net-interoperability-overview-of-phalanger-3-0 http://www.php-compiler.net/blog/2012/net-interoperability-overview-of-phalanger-3-0#comments Sat, 14 Jan 2012 16:35:43 +0000 Miloslav Beňo http://www.php-compiler.net/?p=696 Continue reading ]]> The main goal of Phalanger is to provide fast and easy means for PHP and .NET languages to interoperate with each other. When using Phalanger, PHP becomes a .NET language, but it’s still a dynamic language. That means that, in order to communicate with strongly typed languages as C#, we need more sofisticated architecture. This comes with DLR (Dynamic Language Runtime) and dynamic keyword in C#.

So far we had only few choices how to allow C# and PHP to communicate:

  • Pure mode – PHP gets compiled as standard .NET assembly http://wiki.phpcompiler.net/Pure_mode. Therefore it is easy to use PHP code from C#, but PHP code can’t use global code and inclusions.
  • Duck typing – PHP code can be standard with global code and inclusions. But you have to prepare special strongly-typed interfaces in C#. More details can be found here: http://tomasp.net/blog/ducktyping-in-phalaner.aspx. It is also possible to generate these interfaces from documentation comments.

With Phalanger 3.0 (January 2012) we have one more choice and so far it looks like most convinient one for most of the cases. This is a benefit of incorporating the .NET interoperability model provided by DLR. Using the DLR, it is much easier to call PHP functionality from C# using the dynamic keyword. In one of the previous posts my collegue explains how to use dynamic keyword in C# in combination with Phalanger 2.1. Some of it has changed since, so I want to show how it looks with the current release.

Globals

In Phalanger 3.0 we have implemented convenience dynamic object for simple access to global variables, functions etc. Motivation for this object is that .NET world is fully object-oriented and there is no way to invoke global functions or access global variables from C#.

You can get the instance of the object representing PHP globals from ScriptContext:

dynamic globals = ScriptContext.CurrentContext.Globals;

Following operations are currently available in this object:

  • globals.x - Global variable access.
    This construct assign or read global variable. If variable doesn’t exist it gets created.
    PHP: $x
  • globals.foo(arg1,arg2,...argn) - Global function invocation.
    Global PHP function foo gets invoked with the given arguments and variable is returned (if supplied in PHP function). All the necessary conversions are taken cared of.
    PHP: foo(arg1,arg2,...argn)
  • globals.@const.c Global constant access.
    Global constants can be assigned or readed like this or created if the constant doesn’t exist. The @const syntax is necessary to avoid collisions with variables.
    PHP: c
  • globals.@class.PhpClass(arg1,arg2,...argn) - Creates a new instance.
    The constructor of a class named “PhpClass” is called and new PHP instance is returned which has to be typed as dynamic in C#. Again, the @class syntax is necessary to avoid collisions with global function.
    PHP: new PhpClass(arg1,arg2,...argn)
  • globals.@class.PhpClass.xStatic variable access.
    This construct assigns or reads static class variables of a PHP class named PhpClass.
    PHP: PhpClass::$x
  • globals.@class.PhpClass.foo(arg1,arg2,...argn) - Static method invocation.
    Invokes a static method foo of a PHP class named PhpClass
    PHP: PhpClass::foo(arg1,arg2,...argn)
  • globals.@class.PhpClass.@const.x - Class constant access.
    Class constant can be assigned or read or created if doesn’t exist.
    PHP: PhpClass::x

All the constructs mentioned above work well also if PHP namespaces are used, it’s just necessary to specify the namespace using a special @namespace syntax: globals.@namespace.ns1.ns2.*

For example, globals.@namespace.ns1.ns2.@class.MyClass() – creates an instance of MyClass which is in ns1.ns2 namespace. PHP: new ns1/ns2/PhpClass().

Note: If your PHP is code compiled in pure mode using Phalanger(http://wiki.phpcompiler.net/Pure_mode). You don’t need the globals class at all, as you can access compiled PHP classes directly from C#.

Object Operations

In .NET, an object is the king and although some PHP applications still use procedural approach, there is a plenty of applications built using objects. In this part of the post I’ll explain basic operation on PHP objects that can be used from C# (or any other .NET language that supports DLR).

Create Instance

With globals object explained above you can create a new instance of class X with this construct:

dynamic x = globals.@class.X(arg);

In a previous version of Phalanger, this was also possible, but using a more complex syntax. You could create a new instance of PHP class by calling New method on ScriptContext.

Method Invoke

When we have dynamic object x, we call method just as we are used to in C#.

result = x.Hello(arg1,arg2,...argN);

Arguments can be any value type, reference type or strongly typed delegate which allows you callbacks from PHP code to C#. If PHP object is expected to be returned it has to be assigned to dynamic type.

You can also expect PHP magic method __call to work. If PHP object doesn’t contain method which is called and defined __call method, the method gets invoked with suplied arguments.

The current version of Phalanger does not yet implement the interoperability in the other direction (calling DLR objects from PHP code in Phalanger). This means that dynamic objects defined in C#, IronRuby or IronPython won’t work as expected from PHP code now, only standard CLR objects. This functionality is planed in a future release.

Get and Set Property

Getting or setting properties on PHP objects from C# works as you’d expect.

x.Field = 7;
int i = x.Field;

This works, but you have to be sure that x.Field contains int (or any other type you assign this field to), otherwise runtime exception will be thrown. We are considering implicit conversions with PHP semantics to avoid this in the future. But for the reference types it’s usually sufficient just to use C# operator as.

If you assign to the field that doesn’t exist runtime field is created or if PHP object defines special method __set(), the method will be called with two arguments, name of the assigned field and value to be assigned. Same principle applies when reading a value from field and object defines __get() method.

function __set($name,$value){ }
function __get($name){ }

Invoke on the object

In Phalanger 3.0 it’s possible to invoke PHP object that implements __invoke method or PHP delegate, but how? :-) . The code is surprisingly simple:

dynamic phpObj = globals.@class.PhpClass();
phpObj(arg1,arg2,...,argn);

The method __invoke gets called and any number of arguments can be supplied.

function __invoke($arg1,$arg2,...,argn){}

There’s however one limitation. The object can’t be called if it’s contained as a field of another object.

phpObj(); //this works
y.Field = phpObj;
y.Field(); // this won't work

The reason for this not to work is that we can’t be sure if you want to
invoke a method or invokable field.

Sample

Download the sample

I’ve created small PHP web site as a sample. The web site consits of three important files:

  • default.php – contains definition of PHP class PhpFriend
  • CsFriend.cs – This file is located in App_code folder and contains class CsFriend written in C#. This class gets automatically loaded when the AppPool starts.
  • web.config – Configuration of web site which in
    system.web/compilation/assemblies configration section adds PhpNetCore assembly, so it can be used from C# code

Default.php also contains global code which creates an instance of CsFriend C# class which is in namespace CsLib and calls its method called run.

$cs = new \CsLib\CsFriend();
$cs->run();

The run method just contains few demonstration of the operations I’ve explained here.

First we want to get PHP output stream, which is necessary to use for outputs. Because we want C# to output everything to the same stream as PHP, in order to PHP output control functions to work properly http://www.php.net/manual/en/ref.outcontrol.php . Then we’ll get the Globals object.

TextWriter output = ScriptContext.CurrentContext.Output;
dynamic global = ScriptContext.CurrentContext.Globals;

All the other parts of the sample are with comments so I won’t be explaining them here.

Conclusion

When I say PHP and .NET interoperability, there are two sides of the coin. First one solves how to use .NET in PHP (PHP -> .NET) and the other one solves how to use PHP world from .NET world (.NET -> PHP) which I was explaining in this post. Phalanger 3.0 and .NET with its dynamic type gives us an oportunity to use PHP code from .NET world in a very convinient way.

]]>
http://www.php-compiler.net/blog/2012/net-interoperability-overview-of-phalanger-3-0/feed 0
Passing .NET delegates into PHP http://www.php-compiler.net/blog/2011/pass-delegates-into-php http://www.php-compiler.net/blog/2011/pass-delegates-into-php#comments Sat, 10 Dec 2011 15:33:22 +0000 Jakub Míšek http://www.php-compiler.net/?p=701 Continue reading ]]> Phalanger is the only solution allowing to seamlessly interoperate between PHP and .NET world. It allows to use e.g. .NET generics, namespaces, properties, method overloads, global variables, constants and .NET delegates in both worlds. In this post I will show how to pass delegates from C# into PHP, and how to use them in PHP.

This interoperability feature is available since Phalanger 3.0, and it is very useful e.g. when writing C# plugins for PHP systems.

Imagine you have PHP function foo, getting a callable argument. Now you can call such function from C#, and pass a delegate as you are used to:

x.foo(new Action<int>((int i) =>
{ // do something
}));
x.bar(new Func<int, bool>(this.someMethod));

The conversion to PHP callable function accepts any .NET MulticastDelegate. In this way you can extend functionality of PHP application by passing .NET methods inside. PHP code itself does not need to be changed. It uses .NET delegates as usual PHP callable object; it can be called, copied or passed as callback to any function.

function foo( $func ) {
    $func(123);
}

Demo

To demonstrate the feature, we have a simple demo called ClrDelegates.

The application lets the .NET code to pass some delegates into PHP, and then it calls them to transform given text. It makes use of App_Code feature and other interoperability features that will be described in some future article.

foreach ($callbacks as $fnc)
     $str = $fnc($str);

The usage from within PHP code is very natural, as you can see above.

How does it work

When a MulticastDelegate object is passed from .NET to PHP world, Phalanger wraps the delegate into PHP object with __invoke() method. The __invoke() method is a natural way of creating callable PHP objects. Inside the function, it simply invokes original MulticastDelegate with given arguments. Thanks to this mechanism, PHP code does not even notice, it uses .NET delegates. You can only notice it by checking the class’s name.

Phalanger also checks if you are passing right amount of arguments. Note the types are not implicitly converted yet. .NET may throw an exception if argument’s type does not match the delegate’s type.

Conclusion

Phalanger 3.0 offers easy and useful way of injecting .NET method callbacks into PHP. It is natural to use from within C# and PHP too. Download and try the ClrDelegates  to see how seamlessly it works.

]]>
http://www.php-compiler.net/blog/2011/pass-delegates-into-php/feed 0
Using C# in App_Code from PHP scripts http://www.php-compiler.net/blog/2011/c-sharp-app_code-php http://www.php-compiler.net/blog/2011/c-sharp-app_code-php#comments Mon, 05 Dec 2011 12:00:38 +0000 Jakub Míšek http://www.php-compiler.net/?p=662 Continue reading ]]> Phalanger 3.0 brings several cool and useful features, making interoperability between C# and PHP even easier. In this post, I will show one of them.

Use cases

Imagine several use cases.

  • You have a PHP web site consisting of many .php scripts, and you want to extend its functionality or completely rewrite its back-end into C#. As you are used to, you would like to write your C# code into special App_Code folder.
  • You have ASP.NET web site already. And you are going to add some PHP scripts, because it is easier for your programmers or you have already the presentation layer in PHP. You still have to use existing C# objects from within these PHP scripts.
  • You have PHP web site, and you are going to slowly migrate to C#. You have to subsequently rewrite crucial parts of PHP code into C#, while other parts of your web are still in PHP. This hybrid web has to be working during the process normally.
All these cases have a common architecture. There are .php scripts all over the web site, and .cs scripts in App_Code folder. And you would like to use all your C# methods and classes seamlessly in the PHP code and vice versa.

Setup

Before Phalanger 3.0 release, you would need to write C# code in separated class library project, compile it into an assembly and add the reference into web.config file. This process is being used, however makes development a little slower than it has to be.
Now this process is handled by ASP.NET and Phalanger altogether. You don’t have to configure anything and things do work as expected.
Any public class, written in a script in a .NET language and placed in the App_Code folder is seamlessly visible in your PHP code, using standard PHP syntax.

Demo

I’ve created simple demo here: App_Code interoperability. The demo consists of one PHP page, containing the presentation layer, using Class1 written in C#. Notice there is no configuration linking these two worlds, the things work as it would be expected in any other ASP.NET site.

The demo takes advantage of several features of Phalanger & ASP.NET.

  • The web application remembers the last requested text and amount of requests. It is very easy thanks to the (real) static variables living in instance of ASP.NET AppPool. This technique can be used to achieve very effective in-memory caching.
  • PHP scripts are automatically compiled into .NET assemblies, and automatically recompiled if they change.
  • Backend of the application is easily written in C#, or partially written in C#. In this way crucial parts of the web application can be effectively implemented in C#, while you keep existing code in PHP.
  • Finally C# code in App_Code folder is automatically visible in PHP code, including namespaces, constants, properties and methods.
<b>Camels created: </b><?= Class1::$CamelsProcessed ?><br/>
<b>Previous camel: </b><?= Class1::$LastCamel ?><br/>

<?php
    $x = new Class1;
    $growncamel = $x->MakeCamelFromGet(); // $x->Camel( $_GET['babycamel'] );
    if ($growncamel):
    ?><h2><?= $growncamel ?></h2><?
    endif;
?>

The code above shows simple usage, seamlessly using C# class in PHP code. It really does not seem to use any .NET class, however Class1 is written in C#. It is auto-loaded from App_Code folder.

How does it work

The App_Code thing takes advantage of ASP.NET which automatically pre-compiles all the code in special /App_Code folder. The resulting assembly is placed in the HttpRuntime.CodegenDir (Temporary ASP.NET Files). The assembly name can be found in XML file “App_Code.compiled“, in the XML element “preserve“, attribute “assembly” (if it is not configured differently).

Phalanger itself was able to reference .NET assemblies before. However now it loads the App_Code assembly automatically without a need of an other configuration and compilation.

The App_Code assembly is loaded in compile time. This results in several compile time optimizations, so using such classes is naturally fast. Also when changing the App_Code content Phalanger automatically recompiles PHP scripts.

Conclusion

In this post I tried to demonstrate how easily write a part of PHP web in C#. In this way you can integrate existing C# code base with PHP presentation layer, or just subsequently transform crucial parts of PHP code into C# classes. You can try the ASP.NET website I’ve prepared (App_Code interoperability). It needs just Phalanger 3.0 installed and ASP.NET web server (easiest way is to open Visual Studio 2010, hit File-Open-Web Site, and browse for the folder containing the demo. Hit F5 and run.

]]>
http://www.php-compiler.net/blog/2011/c-sharp-app_code-php/feed 1