PHP as a C # program scripting language (translator) _php Tutorial

Source: Internet
Author: User
Tags dotnet
Original: Http://dotnet.dzone.com/articles/php-scripting-language-c

When we are going to create a. NET program, including a desktop program or Web application, it will certainly be useful if you can use other languages to extend the functionality of this. NET program.

For example, some users can write a simple script to set some settings for this program, or modify how the data is persisted in the program, or write a simple plugin 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 so:
1, many programmers will write some basic PHP code, even a novice programmer can write a simple PHP script code for your application
2, PHP is very easy to use, the network has a lot of ready-made PHP code fragments can be copied directly after the use of
3, thanks to the Phalanger Library (http://phalanger.codeplex.com/), PHP code makes it easy to get any. NET libraries and to invoke services provided by almost all. NET programs

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

Phalanger is a compiler that compiles PHP scripts into. NET bytecode, which is itself designed to allow. NET to seamlessly interoperate with other languages in two directions.
This means that you can call. NET methods in your PHP code and classes that use. NET (http://wiki.phpcompiler.net/.NET_interoperability), and you can also do so in C # or F # PHP methods and classes that use PHP. (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability)
This article also shows another way to use Phalanger: Run PHP code with a. NET program. Especially when 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 is running does not change, Generally you should use a precompiled Script library (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability), This will result in higher efficiency because they will not participate in the compilation at run time.

Configuration

I have tested this technique in the Web 4.0 C # website program, and of course it is possible in a desktop application such as the. NET Console program or WinForms. But remember that your. NET program must be the target. NET Framework using. NET 4.0, and assemblies that must reference at least one phalanger: "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 simplest way is to use the installer.

Source

Incredibly, the core of running PHP code is the PHP.Core.DynamicCode.Eval method, which in the PhpNetCore.dll assembly, the only thing that might be troublesome is the number of parameters required by the method. First we need a usable PHP.Core.ScriptContext instance, which is the execution instance of Phalanger running PHP code. You can get an instance of this from the current thread. Pay special attention to PHP is not multithreaded, so ScriptContext is just tied to a thread

1
var context = PHP. Core.ScriptContext.CurrentContext;
Then we will set the output of the ScriptContext so that the PHP script can convert the stream we need. Here we will set up two output modes-byte stream and text flow. Note that in the end you have to destroy these streams so that all the data will be refreshed correctly

1
Context. OutputStream = output;
2
using (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 PHP code that we are running.
1
Operators.setvariable (context, NULL, "X", "Hello world!");
Eventually we will use the Eval method to run the PHP code. This method is actually used internally by Phalanger to process the eval () expression of PHP. So that's why this method has so many parameters.
01
Evaluate our code:
02
Return Dynamiccode.eval (
03
Code
04
False,/*phalanger Internal stuff*/
05
Context
06
Null,/*local variables*/
07
Null,/*reference to "$this" */
08
Null,/*current class context*/
09
"Default.aspx.cs",/*file name, used for debug and cache key*/
10
1,1,/*position in the file used for debug and cache key*/
11
-1,/*something internal*/
12
Null/*current namespace, used in CLR mode*/
13
);
If the running code behaves like the global PHP code, most of the parameters look nothing special. The most important parameter is code. This parameter is a string that contains your PHP code. Phalanger will first translate and then compile the code. The converted. NET bytecode is stored in memory as a temporary assembly (we also call it an instantaneous assembly)

。 Note that the entire translation and compilation process is fast because the instantaneous assembly is also cached to run the same PHP code faster.

As you can see, you will 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 then 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 (such as defined classes and methods), and the instantaneous assembly can be cached only if the generated PHP code is consistent two times. This is why the parameters in the Eval method code,file name and position match the previous one before being reused after caching.

Let's remember, then, that you should first consider this when you want to run more snippets of PHP code later on.
Finally, if you intend to use Phalanger in a Web application, you should initialize PHP.Core.RequestContext first and then destroy it at the end of the PHP script.
1
using (var Request_context = Requestcontext.initialize (
2
Applicationcontext.default,
3
HttpContext.Current))
4
{/* All the stuff above */}
Summarize:

That's all for a total. You can also use them in. NET code, because the PHP code that is executed later contains the PHP methods, variables, and classes that are already defined.
The language of the. NET application feature. 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.

If you are interested in this and want more information, you can check out the latest standard_mode_interoperability article (Http://wiki.phpcompiler.net/Code_Samples/Standard _mode_interoperability)
Author Junwong's Blog

http://www.bkjia.com/PHPjc/478429.html www.bkjia.com true http://www.bkjia.com/PHPjc/478429.html techarticle Original: Http://dotnet.dzone.com/articles/php-scripting-language-c When we are going to create a. NET program, including a desktop program or a Web application, if you can use a different language to extend ...

  • 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.