dev-master
9999999-dev https://github.com/blyxxyz/Python-PHP-BridgeA way to call PHP code from Python
ISC
The Requires
- php ^7.0
- ext-json *
- ext-reflection *
The Development Requires
by Jan Verbeek
python
Wallogit.com
2017 © Pedro Peláez
A way to call PHP code from Python
This is a Python module for running PHP programs. It lets you import PHP functions, classes, objects, constants and variables to work just like regular Python versions., (*1)
You can call functions:, (*2)
>>> from phpbridge import php
>>> php.array_reverse(['foo', 'bar', 'baz'])
Array.list(['baz', 'bar', 'foo'])
>>> php.echo("foo\n")
foo
>>> php.getimagesize("http://php.net/images/logos/new-php-logo.png")
Array([('0', 200), ('1', 106), ('2', 3), ('3', 'width="200" height="106"'), ('bits', 8), ('mime', 'image/png')])
You can create and use objects:, (*3)
>>> php.DateTime <PHP class 'DateTime'> >>> date = php.DateTime() >>> print(date) <DateTime PHP object (date='2018-05-03 22:59:15.114277', timezone_type=3, timezone='Europe/Berlin')> >>> date.getOffset() 7200 >>> php.ArrayAccess <PHP interface 'ArrayAccess'> >>> issubclass(php.ArrayObject, php.ArrayAccess) True
You can use keyword arguments, even though PHP doesn't support them:, (*4)
>>> date.setDate(year=1900, day=20, month=10) <DateTime PHP object (date='1900-10-20 22:59:15.114277', timezone_type=3, timezone='Europe/Berlin')>
You can loop over iterators and traversables:, (*5)
>>> for path, file in php.RecursiveIteratorIterator(php.RecursiveDirectoryIterator('.git/logs')):
... print("{}: {}".format(path, file.getSize()))
...
.git/logs/.: 16
.git/logs/..: 144
.git/logs/HEAD: 2461
[...]
You can get help:, (*6)
>>> help(php.echo)
Help on function echo:
echo(arg1, *rest)
Output one or more strings.
@param mixed $arg1
@param mixed ...$rest
@return void
You can import namespaces as modules:, (*7)
>>> from phpbridge.php.blyxxyz.PythonServer import NonFunctionProxy >>> help(NonFunctionProxy) Help on class blyxxyz\PythonServer\NonFunctionProxy in module phpbridge.php.blyxxyz.PythonServer: class blyxxyz\PythonServer\NonFunctionProxy(phpbridge.objects.PHPObject) | Provide function-like language constructs as static methods. | | `isset` and `empty` are not provided because it's impossible for a real | function to check whether its argument is defined. | | Method resolution order: | blyxxyz\PythonServer\NonFunctionProxy | phpbridge.objects.PHPObject | builtins.object | | Class methods defined here: | | array(val) -> dict from phpbridge.objects.PHPClass | Cast a value to an array. | | @param mixed $val | | @return array [...]
You can index, and get lengths:, (*8)
>>> arr = php.ArrayObject(['foo', 'bar', 'baz']) >>> arr[10] = 'foobar' >>> len(arr) 4
You can work with PHP's exceptions:, (*9)
>>> try: ... php.get_resource_type(3) ... except php.TypeError as e: ... print(e.getMessage()) ... get_resource_type() expects parameter 1 to be resource, integer given
help is informativehelp works and is informativeSome PHP packages use the same name both for a class and a namespace. As an example, take nikic/PHP-Parser., (*10)
PhpParser\Node is a class, but PhpParser\Node\Param is also a class. This means phpbridge.php.PhpParser.Node becomes ambiguous - it could either refer to the Node class, or the namespace of the Param class., (*11)
In case of such a conflict, the class is preferred over the namespace. To get Param, a from import has to be used:, (*12)
>>> php.require('vendor/autoload.php')
<Composer.Autoload.ClassLoader PHP object (prefixLengthsPsr4=[...: (4)], ...>
>>> import phpbridge.php.PhpParser.Node as Node # Not the namespace!
>>> Node
<PHP interface 'PhpParser\Node'>
>>> from phpbridge.php.PhpParser.Node import Param # The class we want
>>> Param
<PHP class 'PhpParser\Node\Param'>
>>> import phpbridge.php.PhpParser.Node.Param as Param # Doesn't work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'PhpParser\Node' has no attribute 'Param'
If there are no conflicts, things work as expected:, (*13)
>>> from phpbridge.php.blyxxyz.PythonServer import Commands >>> Commands <PHP class 'blyxxyz\PythonServer\Commands'> >>> import phpbridge.php.blyxxyz.PythonServer as PythonServer >>> PythonServer <PHP namespace 'blyxxyz\PythonServer'> >>> PythonServer.Commands <PHP class 'blyxxyz\PythonServer\Commands'>
pip3 install phpbridge
The only dependencies are PHP 7.0+, Python 3.5+, ext-json, ext-reflection and ext-mbstring. Composer can be used to install development tools and set up autoloading, but it's not required., (*14)
A way to call PHP code from Python
ISC
python