Use PHP as a scripting language for C # programs

Source: Internet
Author: User
Tags eval expression interface variables php code php script thread domain name

When we are going to create a. NET program (including a desktop program or a Web application), it would be quite practical if we could expand the functionality of this. NET program with other languages.

For example, some users can write a simple script to set some settings for this program, or to modify the data in the program to persist, or write a simple plug-in for the. NET program. In this article, let's look at how to make PHP a scripting language for. NET programs

Obviously there are many advantages to doing this:

1, a lot of programmers will write some basic PHP code, even a beginner programmer can write a simple PHP script code for your application

2, PHP is very easy to use, the network already has a lot of ready-made PHP code fragments can be copied directly using

3, thanks to the Phalanger Library (http://phalanger.codeplex.com/), PHP code can easily get any. NET libraries and invoke the services provided by almost all. NET programs

The scenario described above is just a small number of cases where PHP code is generated at run time using Phalanger from C # (or other programming languages), for example, can you imagine a Web architecture using C # to write a domain name module and then use PHP to build a user interface. So this article will show you how to run PHP code in a C # program, and how to use global variables as parameters to pass to the PHP code, and how to read the standard. NET stream.

Phalanger is a compiler that compiles PHP scripts into. NET bytecode and is designed to allow seamless interoperability between. NET and other languages.

This means that you can call the. net method in PHP code and use the. NET Class (Http://wiki.phpcompiler.net/.NET_interoperability), and you can also in C # or F # The method of invoking PHP in and the class that uses PHP. (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability)

At the same time, this article shows another way to use Phalanger: Run the PHP code through the. NET program. Especially if the code being run is dynamically acquired or cannot be precompiled into an assembly (for example, when the code is later written by the user). When the PHP code that runs does not change, Typically you should use a precompiled Script library (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability), This can be more efficient because they do not participate in compilation at run time.

Configuration

I have tested this technique in ASP.net 4.0 C # 's Web site program, and of course it is possible in desktop applications such as. NET console programs or WinForms. But keep in mind that your. NET program must be a target. NET Framework using. NET 4.0, and that you must reference at least one Phalanger assembly: "Phpnetcore, version=2.1.0.0, culture= Neutral, publickeytoken=0a8e8c4c76728c71 ". Phalanger must be configured correctly in your application. Although it can be manually configured (Http://www.php-compiler.net/blog/2011/installation-free-phalanger-web), the easiest way is to use the installer.

Source Code

Incredibly, the core of running PHP code is PHP. Core.DynamicCode.Eval This method, which is in the PhpNetCore.dll assembly, the only trouble may be the large number of parameters required by the method. First we need an available PHP.Core.ScriptContext instance, which is the execution instance of the Phalanger running PHP code. You can get one of these instances from the current thread. Pay special attention to PHP is not multithreaded, so ScriptContext is only closely associated with a thread

1var context = PHP. Core.ScriptContext.CurrentContext;

Then we'll set the ScriptContext output so that the PHP script can transform the stream we need. Here we will set two output modes-byte stream and text flow. Notice that at the end you have to destroy these streams so that all the data will be refreshed correctly.

1context. OutputStream = output;

2using (context. Output = new System.IO.StreamWriter (output)) {

We can also set global variables in ScriptContext so that we can easily transfer some parameters to the running PHP code.

1operators.setvariable (context, NULL, "X", "Hello world!");

Eventually we'll use the Eval method to run the PHP code. And this method is actually phalanger internally to handle the eval () expression of PHP. So that's why this method has so many parameters.

01//Evaluate our code:

02return Dynamiccode.eval (

Code,

False,/*phalanger Internal stuff*/

Context,

Null,/*local variables*/

Modified null,/*reference to "$this" * *

Null,/*current class context*/

"Default.aspx.cs",/*file name, used for debug and cache key*/

1,1,/*position in the file used for debug and cache key*/

11-1,/*something internal*/

Null/*current namespace, used in CLR mode*/

13);

If you run your code like the global PHP code, most of the parameters look nothing special. The most important parameter is code. This parameter is a string containing your PHP code. Phalanger will first translate and then compile the code. The converted. NET bytecode is to be stored in memory as a temporary assembly (we also call it an instantaneous assembly)

。 Note that the entire translation and compilation process is quick, as the instantaneous assembly is cached to run the same PHP code.

As you can see, you'll also be able to provide the filename and location of the file in the parameter file name and postion, so when you debug the code and step into the expression, it will just jump to the location specified by the position parameter.

Note whether the cached instantaneous assembly is updated will depend on the PHP code executed earlier in the ScriptContext (for example, the well-defined classes and methods), and the instantaneous assembly can be cached only if the PHP code generated before and after two of times is consistent. This is why the parameter code,file name in the Eval method and the position match to the previous one can be cached and reused.

So let's keep in mind that you should consider this issue first when you're running more snippets of PHP code later.

Finally, if you're going to use Phalanger in a Web application, you should first initialize the PHP.Core.RequestContext and then destroy the PHP script at the end of it.

1using (var request_context = Requestcontext.initialize (

2 Applicationcontext.default,

3 httpcontext.current))

4{/* All the stuff above */}

Summarize:

That's all that's in total. You can also use them in. NET code because the PHP code that is executed later also contains the defined PHP methods, variables, and classes.

The language of. NET application features. You can also use this technique to create a Web application that uses C # to build a domain name module and PHP to build a user interface.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.