Use PHP as the scripting language of the C # Program)

Source: Internet
Author: User

Original article: http://dotnet.dzone.com/articles/php-scripting-language-c
 
When we plan to create.. net Program (including desktop or Web applications), if you can use other languages to expand this.. net program functions will certainly be very useful.
 
For example, some users can write a simple script to set some settings for this program, modify how data is stored persistently in the program, or write a simple plug-in for this. net program. In this article, let's take a look at how php can be used as the scripting language for. net programs.
Obviously, doing so has many advantages:
1. Many programmers 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. There are already a lot of ready-made php code snippets on the network that can be copied and used directly.
3. Thanks to the Phalanger Library (http://phalanger.codeplex.com/), PHP code can easily get any. net Library and call services provided by almost all. net programs
 
The scenario described above is only a small part of the cases where PHP code is generated at runtime using Phalanger from C # (or other programming languages). For example, you can think of what the next web architecture would look like using C # To write a domain name module and then using PHP to build a user interface. so this article will show how to run PHP code in the C # program, how to use global variables as parameters to pass to PHP code, and how to read standards.. net stream.
 
Phalanger is a compiler that compiles PHP scripts into. net bytecode. It is designed to allow two-way interoperability between. net and other languages.
This means that you can call it in php code. net method and usage. net class (http://wiki.phpcompiler.net/.NET_interoperability), at the same time you can call php methods in C # Or F # And Use php class. http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability)
At the same time, this article shows another way to use Phalanger: Through.. net program to run php code. especially when the code to be run is dynamically obtained or cannot be precompiled into an assembly (for example, when the code is later written by the user ). when the running php code does not change, you should generally use the pre-compiled script Library (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability), which can be more efficient because they will not participate in the compilation at runtime.
 
Configuration
 
I have tested this technology in ASP. NET 4.0 C # website programs. Of course, it is also feasible in. net console programs or desktop applications such as winforms. But remember your. net program must be used. net 4.0 (full profile) as the target. net Framework, and must reference at least one Phalanger Assembly: "PhpNetCore, Version = 2.1.0.0, Culture = neutral, PublicKeyToken = 0A8E8C4C76728C71 ". phalanger must be correctly configured in your application. Although it can be configured manually (http://www.php-compiler.net/blog/2011/installation-free-phalanger-web), the easiest way is to use the installer.
 
Source code
 
What is incredible is that the Core of running PHP code is the PHP. Core. DynamicCode. Eval method, which is in the PhpNetCore. dll programming set. The only troublesome thing is the large amount of parameters required by the method. First, we need an available PHP. Core. ScriptContext instance, which is the execution instance of Phalanger running php code. You can obtain such an instance from the current thread. Note that PHP is not multi-threaded, so ScriptContext is only closely associated with a thread.
 
1
Var context = PHP. Core. ScriptContext. CurrentContext;
Then we will set the ScriptContext output mode so that the PHP script can be converted to the desired stream. Here we will set two output modes: byte stream and text stream. Note that at the end you must destroy these streams so that all data will be correctly refreshed.
 
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 transmit some parameters to the running PHP code.
1
Operators. SetVariable (context, null, "X", "Hello World! ");
Finally, we will use the Eval method to run the PHP code. this method is actually used by Phalanger to process PHP eval () expressions. so that is 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 is the same as the global php code, most parameters do not seem special. The most important parameter is code. This parameter is a string containing your php code. Phalanger will first translate and then compile this code. The converted. net bytecode will be stored in the 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 will also be cached and accelerated to run the same PHP code.
 
As you can see, you can also provide the file name and file location in the file name and postion parameters. So when you debug the code and then debug it in a single step to enter the expression, it will jump to the position specified by the position parameter.
Note that whether the cached instantaneous assembly is updated depends on the PHP code executed before ScriptContext (such as defined classes and methods). Only when the PHP code generated twice is consistent, the instantaneous assembly can be cached. This is why the code, file name, and position Parameters in the Eval method can be cached and reused only when they match the previous position.
 
Remember that you should first consider this issue when you want to run more PHP code snippets later.
Finally, if you plan to use Phalanger in a web application, you should first initialize PHP. Core. RequestContext 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 */}
Summary:
 
That's all. Because the PHP code executed later also contains the defined PHP methods, variables, and classes, you can also use them in. net code.
The language of the. net application function. You can also use this technology to create a web application that uses the c # domain name module and PHP to build user interfaces.
 
If you are interested in this and want more information, you can refer to the latest article Standard_mode_interoperability (http://wiki.phpcompiler.net/Code_Samples/Standard_mode_interoperability)
Blog by junwong

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.