All Projects → bullsoft → Phpython

bullsoft / Phpython

PHPython: An extension to eval python3 codes in PHP

Programming Languages

python3
1442 projects
cpp11
221 projects

Projects that are alternatives of or similar to Phpython

RapidPM
High performance extension that implements parts of PocketMine-MP (PMMP) with Zephir
Stars: ✭ 31 (+138.46%)
Mutual labels:  php-extension
raylib-php
PHP 8 Bindings to raylib
Stars: ✭ 112 (+761.54%)
Mutual labels:  php-extension
Zan
高效稳定、安全易用、线上实时验证的全异步高性能网络库,通过PHP扩展方式使用。
Stars: ✭ 453 (+3384.62%)
Mutual labels:  php-extension
Dev-Tools-Magento-2-Module
A collection of utilities meant to improve the experience of developing modules for Magento without breaking existing functionality.
Stars: ✭ 18 (+38.46%)
Mutual labels:  php-extension
panda
A simple extension for PHP
Stars: ✭ 45 (+246.15%)
Mutual labels:  php-extension
Zephir
Zephir is a compiled high level language aimed to the creation of C-extensions for PHP.
Stars: ✭ 3,086 (+23638.46%)
Mutual labels:  php-extension
PHP-CPP-documentation
The documentation in chinese of PHP-CPP.
Stars: ✭ 35 (+169.23%)
Mutual labels:  php-extension
Wasmer Php
🐘🕸️ WebAssembly runtime for PHP
Stars: ✭ 796 (+6023.08%)
Mutual labels:  php-extension
ext-chunkutils2
PHP extension in C++ implementing the modern Minecraft: Bedrock subchunk format for PocketMine-MP 4.0
Stars: ✭ 26 (+100%)
Mutual labels:  php-extension
Php7 Extension Explore
全网唯一PHP7扩展开发教程
Stars: ✭ 402 (+2992.31%)
Mutual labels:  php-extension
phphll
HyperLogLog for PHP implemented as a C extension
Stars: ✭ 19 (+46.15%)
Mutual labels:  php-extension
relay
The next-generation caching layer for PHP.
Stars: ✭ 21 (+61.54%)
Mutual labels:  php-extension
Skyapm Php Sdk
The PHP instrument agent for Apache SkyWalking
Stars: ✭ 292 (+2146.15%)
Mutual labels:  php-extension
php-rar
PECL rar extension
Stars: ✭ 35 (+169.23%)
Mutual labels:  php-extension
Libsodium Php
The PHP extension for libsodium.
Stars: ✭ 507 (+3800%)
Mutual labels:  php-extension
secp256k1-php
PHP bindings for bitcoin-core/secp256k1
Stars: ✭ 55 (+323.08%)
Mutual labels:  php-extension
Study
手把手教你写PHP协程扩展(teach you to write php coroutine extension by hand)
Stars: ✭ 285 (+2092.31%)
Mutual labels:  php-extension
Pinyin Php
`pinyin-php` is a php extension which could translate Chinese character into Chinese PinYin.
Stars: ✭ 26 (+100%)
Mutual labels:  php-extension
Php Opencv
PHP extensions for OpenCV
Stars: ✭ 524 (+3930.77%)
Mutual labels:  php-extension
Php Vips
php binding for libvips
Stars: ✭ 296 (+2176.92%)
Mutual labels:  php-extension

PHPPython

An extension to eval python codes in PHP

Requirements

  • pybind11 V2.1.1
  • PHP-CPP-LEGACY V1.5.7 / PHP-CPP V2.x
  • PHP 5 / PHP 7
  • Python 3
  • C++ 11

Example

Variables defined in Python

$code = <<<EOD
a = [1, 2, 3]
EOD;
$python = new Python();
$python->eval($code);
var_export($python->extract("a"));

... you can use extract method in php to get that python variable, codes above output:

array (
  0 => 1,
  1 => 2,
  2 => 3,
)

Variables defined in PHP

$a = ["a" => "b", "c" => "d"];
$code = <<<EOD
print(a)
EOD;

$python = new Python();
$python->assign("a", $a);
$python->eval($code);

... you can use assign method in php to let python know that php variable, codes above output:

{'a': 'b', 'c': 'd'}

Functions defined in Python

$a = ["a" => "b", "c" => "d"];
$code = <<<EOD
def dofunc(arg1, arg2):
   print("Python Output")
   print(arg1)
   print(arg2)
   return {"Python": {"a": arg1, "b": arg2}}

after = "abcd"

EOD;

$python = new Python();
$python->eval($code);
$python->assign("tmp", $a);
var_dump("PHP Here...", $python->dofunc($a, "py::tmp"));

... you can call functions defined in Python as-is-a $python->method(), codes above output:

Python Output

{'a': 'b', 'c': 'd'}
{'a': 'b', 'c': 'd'}
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}

Here, We can also use these ways to call functions defined in Python:

var_dump("PHP Here...", $python->call("dofunc", [$a, "py::tmp"]));
var_dump("PHP Here...", $python->call("dofunc(after, tmp)"));

this will output:

Python Output
{'a': 'b', 'c': 'd'} // $a in php
{'a': 'b', 'c': 'd'} // tmp in python which assigned by php
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}
Python Output
abcd // after in python
{'a': 'b', 'c': 'd'} // tmp in python which assigned by php
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    string(4) "abcd"
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}

Functions defined in PHP

$code = <<<EOD
tmp = {'a': 'b', 'c': 'd'}
print("Python begin")
print(dofunc(tmp))
print("Python end")
EOD;

$python = new Python();
$python->def("dofunc", function($param) {
    echo __function__ . " in PHP: Get params from Python :" . PHP_EOL;
    echo var_export($param, true) . PHP_EOL;
    return [
        "php" => $param
    ];
});
$python->eval($code);

... you can call php funciton in python as-is-a real python function, codes above output:

Python begin
{closure} in PHP: Get params from Python :
array (
  0 =>
  array (
    'a' => 'b',
    'c' => 'd',
  ),
)
{'php': [{'a': 'b', 'c': 'd'}]}
Python end

if you like, you can also call that function using the php way, like this:

var_dump($python->dofunc("py::tmp", "phpString"));

this will output:

{closure} in PHP: Get params from Python :
array (
  0 =>
  array (
    'a' => 'b',
    'c' => 'd',
  ),
  1 => 'phpString',
)
array(1) {
  'php' =>
  array(2) {
    [0] =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    [1] =>
    string(9) "phpString"
  }
}

PHP Call Python Function

PHP Call Python Function

Python Call PHP Function

Python Call PHP Function

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].