SlideShare a Scribd company logo
PH7 Engine Introduction: http://ph7.symisc.net




      Introduction To Embedding PH7 PHP Engine in a C/C++ Host Application.
                                     November 08, 2012
                                    http://ph7.symisc.net/




                       Author: Mrad Chems Eddine <chm@symisc.net>




Copyright © Symisc Systems, SUARL
PH7 Engine Introduction: http://ph7.symisc.net




What is PH7
PH7 is a in-process software library that implements a highly-efficient embeddable bytecode
compiler and a virtual machine for the PHP programming language. In other words, PH7 is a
PHP engine which allow the host application to compile and execute PHP scripts in-process.
PH7 is to PHP what SQLite is to SQL.
PH7 implements most of the constructs introduced by the PHP 5.3 release such as heredoc,
nowdoc, gotos, classes, anonymous functions, closures and so on and introduces very powerful
extensions to the PHP programming language such as:

    •   Function & Method Overloading.
    •   Full Type Hinting.
    •   Introducing comma expressions.
    •   Introducing the eq and ne operators for strict string comparison.
    •   Improved operators precedences.
    •   Powerful OO subsystem.
    •   Function arguments can take any complex expressions as their default values.
    •   64-bit integer arithmetic for all platforms.
    •   Native UTF-8 support.
    •   PH7 is 100% hand-coded, written in ANSI C, compile and run unmodified in any
        platform including restricted embedded devices with a C compiler.
    •   Amalgamation: All C source code for PH7 are combined into a single source file.
    •   Built with more 470 function including an XML parser (with namespace support), INI
        processor, CSV reader/writer, UTF-8 encoder/decoder, zip archive extractor, JSON
        encoder/decoder, random number/strings generator, native and efficient File IO for
        Windows and UNIX systems and many more without the need of any external library to
        link with.
    •   PH7 is an Open-Source product.
                                            Refer to the feature page for a detailed description.



PH7 is the ideal library for enhancing your application (i.e: SCM, Server, CMS, Control panel,
Search engine, etc.) or device (i.e: router, set-top box, etc.) with dynamic web interfaces, with
all benefits and power of the PHP(5) programming language without the overhead uncured by
the insecure CGI mechanism and it's high costs such as process creation (fork(), exec()) since
PH7 run in-process.


As an embedded interpreter, it allows multiple interpreter states to coexist in the same
program, without any interference between them. Programmatically, foreign functions in C can
be added and values can be defined in the PHP environment. Being a quite small program, it is
easy to comprehend, get to grips with, and use.


PH7 is 100% hand-coded, written in ANSI C, compiles unmodified and should run in any
platform including restricted embedded device with a C compiler.
PH7 is extensively tested on Windows and UNIX systems especially Linux, FreeBSD, Oracle
Solaris and Mac OS X.



Copyright © Symisc Systems, SUARL
PH7 Engine Introduction: http://ph7.symisc.net


PH7 is a compact library. With all features enabled, the library size can be less than 600KiB,
depending on compiler optimization settings. (Some compiler optimizations such as aggressive
function inlining and loop unrolling can cause the object code to be much larger.) If optional
features are omitted, the size of the PH7 library can be reduced below 220KiB. PH7 can also be
made to run in very little heap (2MB), making PH7 a popular PHP engine choice on memory
constrained gadgets such as cellphones, tablets, numeric devices and so on.

PH7 is an open-source, dual-licensed product available free of charge for open-source projects
under the term of the Symisc Public License (SPL) which is a GPL compatible license and is
equivalent to the more popular Sleepycat license (An OSI approved license). See the
licensing page for additional information.

PH7 in 5 Minutes or Less
Here is what you do to start experimenting with the PH7 engine without having to do a lot of
tedious reading and configuration:


download The Code
Get a copy of the last public release of the PH7 engine. Visit the download page for more
information.

Write Programs That Use PH7
Below is a simple C program that demonstrates how to use the C/C++ interface to PH7. This
program compile and execute the following PHP script.




          <?php
            echo 'Welcome guest'.PHP_EOL;
            echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;
            echo 'and you are running '.php_uname();
          ?>



That is, this simple PHP script when running should display a greeting message, the current
system time and the host operating system. A typical output of this program would look like
this:


              Welcome guest
              Current system time is: 2012-09-14 10:08:44
              and you are running Microsoft Windows 7 localhost 6.1 build 7600 x86




Here is the C code. Note that you can get a working version of this program here:




Copyright © Symisc Systems, SUARL
PH7 Engine Introduction: http://ph7.symisc.net


              1. #include "ph7.h"
              2. int main(void)
              3. {
              4. ph7 *pEngine; /* PH7 engine */
              5. ph7_vm *pVm; /* Compiled PHP program */
              6. int rc;
              7. /* Allocate a new PH7 engine instance */
              8. rc = ph7_init(&pEngine);
              9. if( rc != PH7_OK ){
              10. /*
              11. * If the supplied memory subsystem is so sick that we are unable
              12. * to allocate a tiny chunk of memory, there is no much we can do here.
              13. */
              14. Fatal("Error while allocating a new PH7 engine instance");
              15. }
              16. /* Compile the PHP test program defined above */
              17. rc = ph7_compile_v2(
              18.     pEngine, /* PH7 engine */
              19.     PHP_PROG, /* PHP test program */
              20.     -1 /* Compute input length automatically*/,
              21.     &pVm, /* OUT: Compiled PHP program */
              22.     0 /* IN: Compile flags */
              23. );
              24. if( rc != PH7_OK ){
              25. if( rc == PH7_COMPILE_ERR ){
              26.     const char *zErrLog;
              27.     int nLen;
              28.    /* Extract error log */
              29.     ph7_config(pEngine,
              30.       PH7_CONFIG_ERR_LOG,
              31.       &zErrLog,
              32.       &nLen
              33.    );
              34. if( nLen > 0 ){
              35.    /* zErrLog is null terminated */
              36.    puts(zErrLog);
              37. }
              38. }
              39. /* Exit */
              40. Fatal("Compile error");
              41.}
              42./*
              43. * Now we have our script compiled, it's time to configure our VM.
              44. * We will install an output consumer callback that redirect
              45. * the VM output to STDOUT (download the C file to see the implementation).
              46. */
              47.rc = ph7_vm_config(pVm,
              48.        PH7_VM_CONFIG_OUTPUT,
              49.       Output_Consumer, /* Output Consumer callback */
              50.       0 /* Callback private data */
              51. );
              52. if( rc != PH7_OK ){
              53.    Fatal("Error while installing the VM output consumer callback");
              54. }

Copyright © Symisc Systems, SUARL
PH7 Engine Introduction: http://ph7.symisc.net


              55./*
              56.* And finally, execute our program. Note that your output (STDOUT in our
                 case)
              57.* should display the result.
              58.*/
              59. ph7_vm_exec(pVm,0);
              60./* All done, cleanup the mess left behind.
              61.*/
              62. ph7_vm_release(pVm);
              63. ph7_release(pEngine);
              64.return 0;
              65.}
                                                                          Download the C file.
We create a new PH7 engine instance using a call to ph7_init() on line 8. This is often the first
PH7 API call that an application makes and is a prerequisite in order to compile PHP code using
one of the compile interfaces.
We compile our PHP test program on line 17 using the ph7_compile_v2() interface.
We configure our Virtual Machine on line 47 by setting a VM output consumer callback named
Output_Consumer() (Download the C file to see the implementation). All this callback does is
redirecting the VM output to STDOUT using the libc printf() routine or the write() system call.
And finally we execute our PHP program on line 59 using a call to ph7_vm_exec(). You should
see now the greeting message, the current date and the host operating system.
Clean-up is done on line 62 and 63 respectively via calls to ph7_vm_release() and
ph7_release().


Compile the program
Compile this C file together with the PH7 engine source code to generate the executable. For
example:
                      gcc -W -Wall -O6 -o ph7_test ph7_intro.c ph7.c
When running [./ph7_test ] you should see the greeting message, the current system time and
the host operating system.


Stand-alone Interpreter For PH7
The PH7 download page includes a simple stand-alone PHP interpreter named ph7 (or
ph7.exe on windows) that allows the user to enter and execute PHP files against a PH7
engine. This utility is available in prebuilt binaries forms or can be compiled from source. You
can get a copy of the PH7 interpreter from the download page.
To start the ph7 program, just type "ph7" followed by the name of the PHP file to compile and
execute. That is, the first argument is to the interpreter, the rest are scripts arguments, press
"Enter" and the PHP code will be executed.
If something goes wrong while processing the PHP script due to a compile-time error, your
error output (STDOUT) should display the compile-time error messages.


Usage example of the ph7 interpreter:
Running the interpreter
ph7 scripts/hello_world.php


Copyright © Symisc Systems, SUARL
PH7 Engine Introduction: http://ph7.symisc.net


Running the interpreter with script arguments
ph7 scripts/mp3_tag.php /usr/local/path/to/my_mp3s


The PH7 interpreter package includes more than 70 PHP scripts to test ranging from simple
hello world programs to XML processing, zip archive extracting, MP3 tag extracting, UUID
generation, JSON encoding/decoding, INI processing, Base32 encoding/decoding and many
more. These scripts are available in the scripts directory from the zip archive.


Next
Check out the Introduction To The PH7 C/C++ Interface for an introductory overview and
roadmap to the dozens of PH7 interface functions.
A separate document, The PH7 C/C++ Interface, provides detailed specifications for all of the
various C/C++ APIs for PH7. Once the reader understands the basic principles of operation for
PH7, that document should be used as a reference guide.
Any questions, check the Frequently Asked Questions page or visit the Support Page for online
community support.
                                         PH7 Engine Homepage: http://ph7.symisc.net/




Copyright © Symisc Systems, SUARL

More Related Content

What's hot

OpenFOAM 2.4.0 installation on CentOS-7
OpenFOAM 2.4.0 installation on CentOS-7OpenFOAM 2.4.0 installation on CentOS-7
OpenFOAM 2.4.0 installation on CentOS-7Guy Tel-Zur
 
HKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewHKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewLinaro
 
Source pack installation of OpenFOAM.4.0 into RHL
Source pack installation of OpenFOAM.4.0 into RHLSource pack installation of OpenFOAM.4.0 into RHL
Source pack installation of OpenFOAM.4.0 into RHLmmer547
 
Composing and Executing Parallel Data Flow Graphs wth Shell Pipes
Composing and Executing Parallel Data Flow Graphs wth Shell PipesComposing and Executing Parallel Data Flow Graphs wth Shell Pipes
Composing and Executing Parallel Data Flow Graphs wth Shell PipesVinoth Chandar
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT CompilerNetronome
 
eBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureeBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureNetronome
 
6 open capi_meetup_in_japan_final
6 open capi_meetup_in_japan_final6 open capi_meetup_in_japan_final
6 open capi_meetup_in_japan_finalYutaka Kawai
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in ElixirAaron Seigo
 
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im ÜberblickEin Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblickrenebruns
 
L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5William Lee
 
4. open mano set up and usage
4. open mano set up and usage4. open mano set up and usage
4. open mano set up and usagevideos
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成zhongbing liu
 
PRoot improved kernel compatibility
PRoot improved kernel compatibilityPRoot improved kernel compatibility
PRoot improved kernel compatibilitycvinc02
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu WorksZhen Wei
 
20111011 bigbluebutton
20111011 bigbluebutton20111011 bigbluebutton
20111011 bigbluebuttonhs1250
 
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFTaeung Song
 

What's hot (20)

OpenFOAM 2.4.0 installation on CentOS-7
OpenFOAM 2.4.0 installation on CentOS-7OpenFOAM 2.4.0 installation on CentOS-7
OpenFOAM 2.4.0 installation on CentOS-7
 
HKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewHKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting Review
 
Source pack installation of OpenFOAM.4.0 into RHL
Source pack installation of OpenFOAM.4.0 into RHLSource pack installation of OpenFOAM.4.0 into RHL
Source pack installation of OpenFOAM.4.0 into RHL
 
Divorcing System
Divorcing SystemDivorcing System
Divorcing System
 
Composing and Executing Parallel Data Flow Graphs wth Shell Pipes
Composing and Executing Parallel Data Flow Graphs wth Shell PipesComposing and Executing Parallel Data Flow Graphs wth Shell Pipes
Composing and Executing Parallel Data Flow Graphs wth Shell Pipes
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT Compiler
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
eBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureeBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging Infrastructure
 
6 open capi_meetup_in_japan_final
6 open capi_meetup_in_japan_final6 open capi_meetup_in_japan_final
6 open capi_meetup_in_japan_final
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in Elixir
 
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im ÜberblickEin Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
 
L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5
 
4. open mano set up and usage
4. open mano set up and usage4. open mano set up and usage
4. open mano set up and usage
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
 
PRoot improved kernel compatibility
PRoot improved kernel compatibilityPRoot improved kernel compatibility
PRoot improved kernel compatibility
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
 
Secure PHP environment
Secure PHP environmentSecure PHP environment
Secure PHP environment
 
20111011 bigbluebutton
20111011 bigbluebutton20111011 bigbluebutton
20111011 bigbluebutton
 
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
 

Similar to Introduction To Embedding The PH7 PHP Engine in a C/C++ Host Application.

Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerJackson F. de A. Mafra
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Max Kleiner
 
Complete MPICH2 Clustering Manual in Ubuntu
Complete MPICH2 Clustering Manual in UbuntuComplete MPICH2 Clustering Manual in Ubuntu
Complete MPICH2 Clustering Manual in UbuntuMinhas Kamal
 
Php internal-release 2011-04-01-v0.5.2
Php internal-release 2011-04-01-v0.5.2Php internal-release 2011-04-01-v0.5.2
Php internal-release 2011-04-01-v0.5.2PL dream
 
PHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsPHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsAlessandro Pilotti
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8Wim Godden
 
Cell processor lab
Cell processor labCell processor lab
Cell processor labcoolmirza143
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extensionhazzaz
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Max Kleiner
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshopjulien pauli
 
PHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudPHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudSalesforce Developers
 

Similar to Introduction To Embedding The PH7 PHP Engine in a C/C++ Host Application. (20)

Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant Killer
 
Php ppt
Php pptPhp ppt
Php ppt
 
Howto Pxeboot
Howto PxebootHowto Pxeboot
Howto Pxeboot
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475
 
Phalcon - Giant Killer
Phalcon - Giant KillerPhalcon - Giant Killer
Phalcon - Giant Killer
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Complete MPICH2 Clustering Manual in Ubuntu
Complete MPICH2 Clustering Manual in UbuntuComplete MPICH2 Clustering Manual in Ubuntu
Complete MPICH2 Clustering Manual in Ubuntu
 
Php internal-release 2011-04-01-v0.5.2
Php internal-release 2011-04-01-v0.5.2Php internal-release 2011-04-01-v0.5.2
Php internal-release 2011-04-01-v0.5.2
 
PHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsPHP and FastCGI Performance Optimizations
PHP and FastCGI Performance Optimizations
 
CodeShip
CodeShipCodeShip
CodeShip
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8
 
Php
PhpPhp
Php
 
Cell processor lab
Cell processor labCell processor lab
Cell processor lab
 
Phalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil ConferencePhalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil Conference
 
LIGGGHTS installation-guide
LIGGGHTS installation-guideLIGGGHTS installation-guide
LIGGGHTS installation-guide
 
HPC_MPI_CICD.pptx
HPC_MPI_CICD.pptxHPC_MPI_CICD.pptx
HPC_MPI_CICD.pptx
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extension
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
PHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudPHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the Cloud
 

Recently uploaded

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform EngineeringJemma Hussein Allen
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
 

Recently uploaded (20)

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 

Introduction To Embedding The PH7 PHP Engine in a C/C++ Host Application.

  • 1. PH7 Engine Introduction: http://ph7.symisc.net Introduction To Embedding PH7 PHP Engine in a C/C++ Host Application. November 08, 2012 http://ph7.symisc.net/ Author: Mrad Chems Eddine <chm@symisc.net> Copyright © Symisc Systems, SUARL
  • 2. PH7 Engine Introduction: http://ph7.symisc.net What is PH7 PH7 is a in-process software library that implements a highly-efficient embeddable bytecode compiler and a virtual machine for the PHP programming language. In other words, PH7 is a PHP engine which allow the host application to compile and execute PHP scripts in-process. PH7 is to PHP what SQLite is to SQL. PH7 implements most of the constructs introduced by the PHP 5.3 release such as heredoc, nowdoc, gotos, classes, anonymous functions, closures and so on and introduces very powerful extensions to the PHP programming language such as: • Function & Method Overloading. • Full Type Hinting. • Introducing comma expressions. • Introducing the eq and ne operators for strict string comparison. • Improved operators precedences. • Powerful OO subsystem. • Function arguments can take any complex expressions as their default values. • 64-bit integer arithmetic for all platforms. • Native UTF-8 support. • PH7 is 100% hand-coded, written in ANSI C, compile and run unmodified in any platform including restricted embedded devices with a C compiler. • Amalgamation: All C source code for PH7 are combined into a single source file. • Built with more 470 function including an XML parser (with namespace support), INI processor, CSV reader/writer, UTF-8 encoder/decoder, zip archive extractor, JSON encoder/decoder, random number/strings generator, native and efficient File IO for Windows and UNIX systems and many more without the need of any external library to link with. • PH7 is an Open-Source product. Refer to the feature page for a detailed description. PH7 is the ideal library for enhancing your application (i.e: SCM, Server, CMS, Control panel, Search engine, etc.) or device (i.e: router, set-top box, etc.) with dynamic web interfaces, with all benefits and power of the PHP(5) programming language without the overhead uncured by the insecure CGI mechanism and it's high costs such as process creation (fork(), exec()) since PH7 run in-process. As an embedded interpreter, it allows multiple interpreter states to coexist in the same program, without any interference between them. Programmatically, foreign functions in C can be added and values can be defined in the PHP environment. Being a quite small program, it is easy to comprehend, get to grips with, and use. PH7 is 100% hand-coded, written in ANSI C, compiles unmodified and should run in any platform including restricted embedded device with a C compiler. PH7 is extensively tested on Windows and UNIX systems especially Linux, FreeBSD, Oracle Solaris and Mac OS X. Copyright © Symisc Systems, SUARL
  • 3. PH7 Engine Introduction: http://ph7.symisc.net PH7 is a compact library. With all features enabled, the library size can be less than 600KiB, depending on compiler optimization settings. (Some compiler optimizations such as aggressive function inlining and loop unrolling can cause the object code to be much larger.) If optional features are omitted, the size of the PH7 library can be reduced below 220KiB. PH7 can also be made to run in very little heap (2MB), making PH7 a popular PHP engine choice on memory constrained gadgets such as cellphones, tablets, numeric devices and so on. PH7 is an open-source, dual-licensed product available free of charge for open-source projects under the term of the Symisc Public License (SPL) which is a GPL compatible license and is equivalent to the more popular Sleepycat license (An OSI approved license). See the licensing page for additional information. PH7 in 5 Minutes or Less Here is what you do to start experimenting with the PH7 engine without having to do a lot of tedious reading and configuration: download The Code Get a copy of the last public release of the PH7 engine. Visit the download page for more information. Write Programs That Use PH7 Below is a simple C program that demonstrates how to use the C/C++ interface to PH7. This program compile and execute the following PHP script. <?php echo 'Welcome guest'.PHP_EOL; echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL; echo 'and you are running '.php_uname(); ?> That is, this simple PHP script when running should display a greeting message, the current system time and the host operating system. A typical output of this program would look like this: Welcome guest Current system time is: 2012-09-14 10:08:44 and you are running Microsoft Windows 7 localhost 6.1 build 7600 x86 Here is the C code. Note that you can get a working version of this program here: Copyright © Symisc Systems, SUARL
  • 4. PH7 Engine Introduction: http://ph7.symisc.net 1. #include "ph7.h" 2. int main(void) 3. { 4. ph7 *pEngine; /* PH7 engine */ 5. ph7_vm *pVm; /* Compiled PHP program */ 6. int rc; 7. /* Allocate a new PH7 engine instance */ 8. rc = ph7_init(&pEngine); 9. if( rc != PH7_OK ){ 10. /* 11. * If the supplied memory subsystem is so sick that we are unable 12. * to allocate a tiny chunk of memory, there is no much we can do here. 13. */ 14. Fatal("Error while allocating a new PH7 engine instance"); 15. } 16. /* Compile the PHP test program defined above */ 17. rc = ph7_compile_v2( 18. pEngine, /* PH7 engine */ 19. PHP_PROG, /* PHP test program */ 20. -1 /* Compute input length automatically*/, 21. &pVm, /* OUT: Compiled PHP program */ 22. 0 /* IN: Compile flags */ 23. ); 24. if( rc != PH7_OK ){ 25. if( rc == PH7_COMPILE_ERR ){ 26. const char *zErrLog; 27. int nLen; 28. /* Extract error log */ 29. ph7_config(pEngine, 30. PH7_CONFIG_ERR_LOG, 31. &zErrLog, 32. &nLen 33. ); 34. if( nLen > 0 ){ 35. /* zErrLog is null terminated */ 36. puts(zErrLog); 37. } 38. } 39. /* Exit */ 40. Fatal("Compile error"); 41.} 42./* 43. * Now we have our script compiled, it's time to configure our VM. 44. * We will install an output consumer callback that redirect 45. * the VM output to STDOUT (download the C file to see the implementation). 46. */ 47.rc = ph7_vm_config(pVm, 48. PH7_VM_CONFIG_OUTPUT, 49. Output_Consumer, /* Output Consumer callback */ 50. 0 /* Callback private data */ 51. ); 52. if( rc != PH7_OK ){ 53. Fatal("Error while installing the VM output consumer callback"); 54. } Copyright © Symisc Systems, SUARL
  • 5. PH7 Engine Introduction: http://ph7.symisc.net 55./* 56.* And finally, execute our program. Note that your output (STDOUT in our case) 57.* should display the result. 58.*/ 59. ph7_vm_exec(pVm,0); 60./* All done, cleanup the mess left behind. 61.*/ 62. ph7_vm_release(pVm); 63. ph7_release(pEngine); 64.return 0; 65.} Download the C file. We create a new PH7 engine instance using a call to ph7_init() on line 8. This is often the first PH7 API call that an application makes and is a prerequisite in order to compile PHP code using one of the compile interfaces. We compile our PHP test program on line 17 using the ph7_compile_v2() interface. We configure our Virtual Machine on line 47 by setting a VM output consumer callback named Output_Consumer() (Download the C file to see the implementation). All this callback does is redirecting the VM output to STDOUT using the libc printf() routine or the write() system call. And finally we execute our PHP program on line 59 using a call to ph7_vm_exec(). You should see now the greeting message, the current date and the host operating system. Clean-up is done on line 62 and 63 respectively via calls to ph7_vm_release() and ph7_release(). Compile the program Compile this C file together with the PH7 engine source code to generate the executable. For example: gcc -W -Wall -O6 -o ph7_test ph7_intro.c ph7.c When running [./ph7_test ] you should see the greeting message, the current system time and the host operating system. Stand-alone Interpreter For PH7 The PH7 download page includes a simple stand-alone PHP interpreter named ph7 (or ph7.exe on windows) that allows the user to enter and execute PHP files against a PH7 engine. This utility is available in prebuilt binaries forms or can be compiled from source. You can get a copy of the PH7 interpreter from the download page. To start the ph7 program, just type "ph7" followed by the name of the PHP file to compile and execute. That is, the first argument is to the interpreter, the rest are scripts arguments, press "Enter" and the PHP code will be executed. If something goes wrong while processing the PHP script due to a compile-time error, your error output (STDOUT) should display the compile-time error messages. Usage example of the ph7 interpreter: Running the interpreter ph7 scripts/hello_world.php Copyright © Symisc Systems, SUARL
  • 6. PH7 Engine Introduction: http://ph7.symisc.net Running the interpreter with script arguments ph7 scripts/mp3_tag.php /usr/local/path/to/my_mp3s The PH7 interpreter package includes more than 70 PHP scripts to test ranging from simple hello world programs to XML processing, zip archive extracting, MP3 tag extracting, UUID generation, JSON encoding/decoding, INI processing, Base32 encoding/decoding and many more. These scripts are available in the scripts directory from the zip archive. Next Check out the Introduction To The PH7 C/C++ Interface for an introductory overview and roadmap to the dozens of PH7 interface functions. A separate document, The PH7 C/C++ Interface, provides detailed specifications for all of the various C/C++ APIs for PH7. Once the reader understands the basic principles of operation for PH7, that document should be used as a reference guide. Any questions, check the Frequently Asked Questions page or visit the Support Page for online community support. PH7 Engine Homepage: http://ph7.symisc.net/ Copyright © Symisc Systems, SUARL