1. Go to this page and download the library: Download pixelfederation/z-engine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pixelfederation / z-engine example snippets
use ZEngine\Core;
declare(strict_types=1);
use ZEngine\Reflection\ReflectionClass;
class);
$refClass->setFinal(false);
eval('class TestClass extends FinalClass {}'); // Should be created
use ZEngine\Core;
$instance = new stdClass();
$handle = spl_object_id($instance);
$objectEntry = Core::$executor->objectStore[$handle];
var_dump($objectEntry);
use ZEngine\ClassExtension\ObjectCastInterface;
use ZEngine\ClassExtension\ObjectCompareValuesInterface;
use ZEngine\ClassExtension\ObjectCreateInterface;
use ZEngine\ClassExtension\ObjectCreateTrait;
use ZEngine\ClassExtension\ObjectDoOperationInterface;
class Matrix implements
ObjectCreateInterface,
ObjectCompareValuesInterface,
ObjectDoOperationInterface,
ObjectCastInterface
{
use ObjectCreateTrait;
// ...
}
$a = new Matrix([10, 20, 30]);
$b = new Matrix([1, 2, 3]);
$c = $a + $b; // Matrix([11, 22, 33])
$c *= 2; // Matrix([22, 44, 66])
use ZEngine\Reflection\ReflectionClass as ReflectionClassEx;
// ... initialization logic
$refClass = new ReflectionClassEx(Matrix::class);
$refClass->installExtensionHandlers();
use ZEngine\ClassExtension\ObjectCreateTrait;
use ZEngine\Reflection\ReflectionClass as ReflectionClassEx;
$refClass = new ReflectionClassEx(Matrix::class);
$handler = Closure::fromCallable([ObjectCreateTrait::class, '__init']);
$refClass->setCreateObjectHandler($handler);
$refClass->setCompareValuesHandler(function ($left, $right) {
if (is_object($left)) {
$left = spl_object_id($left);
}
if (is_object($right)) {
$right = spl_object_id($right);
}
// Just for example, object with bigger object_id is considered bigger that object with smaller object_id
return $left <=> $right;
});
use ZEngine\ClassExtension\Hook\CastObjectHook;
/**
* Interface ObjectCastInterface allows to cast given object to scalar values, like integer, floats, etc
*/
interface ObjectCastInterface
{
/**
* Performs casting of given object to another value
*
* @param CastObjectHook $hook Instance of current hook
*
* @return mixed Casted value
*/
public static function __cast(CastObjectHook $hook);
}
use ZEngine\ClassExtension\Hook\CompareValuesHook;
/**
* Interface ObjectCompareValuesInterface allows to perform comparison of objects
*/
interface ObjectCompareValuesInterface
{
/**
* Performs comparison of given object with another value
*
* @param CompareValuesHook $hook Instance of current hook
*
* @return int Result of comparison: 1 is greater, -1 is less, 0 is equal
*/
public static function __compare(CompareValuesHook $hook): int;
}
use ZEngine\ClassExtension\Hook\DoOperationHook;
/**
* Interface ObjectDoOperationInterface allows to perform math operations (aka operator overloading) on object
*/
interface ObjectDoOperationInterface
{
/**
* Performs an operation on given object
*
* @param DoOperationHook $hook Instance of current hook
*
* @return mixed Result of operation value
*/
public static function __doOperation(DoOperationHook $hook);
}
use ZEngine\Core;
use ZEngine\System\Hook\AstProcessHook;
Core::setASTProcessHandler(function (AstProcessHook $hook) {
$ast = $hook->getAST();
echo "Parsed AST:", PHP_EOL, $ast->dump();
// Let's modify Yes to No )
echo $ast->getChild(0)->getChild(0)->getChild(0)->getValue()->setNativeValue('No');
});
eval('echo "Yes";');
// Parsed AST:
// 1: AST_STMT_LIST
// 1: AST_STMT_LIST
// 1: AST_ECHO
// 1: AST_ZVAL string('Yes')
// No
use ZEngine\EngineExtension\AbstractModule;
class SimpleCountersModule extends AbstractModule
{
/**
* Returns the target thread-safe mode for this module
*
* Use ZEND_THREAD_SAFE as default if your module does not depend on thread-safe mode.
*/
public static function targetThreadSafe(): bool
{
return ZEND_THREAD_SAFE;
}
/**
* Returns the target debug mode for this module
*
* Use ZEND_DEBUG_BUILD as default if your module does not depend on debug mode.
*/
public static function targetDebug(): bool
{
return ZEND_DEBUG_BUILD;
}
/**
* Returns the target API version for this module
*
* @see zend_modules.h:ZEND_MODULE_API_NO
*/
public static function targetApiVersion(): int
{
return 20190902;
}
/**
* Returns true if this module should be persistent or false if temporary
*/
public static function targetPersistent(): bool
{
return true;
}
/**
* Returns globals type (if present) or null if module doesn't use global memory
*/
public static function globalType(): ?string
{
return 'unsigned int[10]';
}
}
$module = new SimpleCountersModule();
if (!$module->isModuleRegistered()) {
$module->register();
$module->startup();
}
$data = $module->getGlobals();
var_dump($data);
$index = mt_rand(0, 9); // If you have several workers, you should use worker pid to avoid race conditions
$data[$index] = $data[$index] + 1; // We are increasing global counter by one
/* Example of var_dump after several requests...
object(FFI\CData:uint32_t[10])#35 (10) {
[0]=>
int(1)
[1]=>
int(1)
[2]=>
int(1)
[3]=>
int(3)
[4]=>
int(1)
[5]=>
int(1)
[6]=>
int(1)
[7]=>
int(2)
[8]=>
int(2)
[9]=>
int(2)
}*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.