PHP interaction with other languages-summary the original website will be updated constantly: Tsung's technical blog "PHP interaction with other languages-summary" it.zuocheng.net
Different modules of software or system may be written in different languages, and there will inevitably be problems of interaction and communication between different languages. From the perspective of PHP, This article explores how PHP interacts with other languages.
Language layer interaction using glue language
Glue language Shell scripts Python Ruby Lua Tcl Perl.
In essence, it is to call the input and output of an operating system. the operating system creates a new process for an application written in other languages and then returns the output to PHP.
It is easy to implement.
Disadvantages:
System calls are at risk;
Each call to the system creates a new process with high overhead
Complex data structures cannot be passed directlyCompile PHP extension
Encapsulate the APIs provided by other languages into C interfaces (often compiled as dynamic link libraries) and encapsulate them in PHP extensions so that they can be directly called in PHP statements.
Advantages:
High performance
It can realize interaction between complex structure data.
Disadvantages:
High development and O & M costsUse PHP Native Interface (PNI)
PNI is a PHP extension that allows PHP code to directly call C class libraries.
Use SWIG
The ultimate goal is to compile PHP Extensions. The difference is that the interfaces provided by SWIG allow developers to save learning costs compared with Zend APIs and PHP APIs.
Inter-process interaction
If you cannot directly interact with each other at the language level, you can use the interaction method between processes. The essence is to call the methods provided by the operating system for inter-process interaction.
Signal
Semaphores
MPs queue
Message Queue
Shared memory
Files, memory databases, etc.Inter-system interaction
If systems written in different languages interact with each other, you can use the following methods:
Network call (Http, Socket, Web Service)
Database
Data files
Distributed Systems (Memcache, Radis, rabitMQ)