1. Go to this page and download the library: Download stillat/primitives 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/ */
stillat / primitives example snippets
use Stillat\Primitives\Parser;
$parser = new Parser();
$result = $parser->parseString('[1, 2, 3], "some-string", "another", ["one" => 1, "two" => 2]');
use Stillat\Primitives\Parser;
$parser = new Parser();
$result = $parser->parseMethod('methodName([1, 2, 3])');
use Stillat\Primitives\Parser;
$parser = new Parser();
$result = $parser->parseMethods("randomElements(['a', 'b', 'c', 'd', 'e'], rand(1, 5))");
use Stillat\Primitives\Parser;
use Stillat\Primitives\MethodRunner;
$parser = new Parser();
$runner = new MethodRunner();
class MyClass {
public function sayHello($name)
{
return 'Hello, '.$name;
}
}
$myClassInstance = new MyClass();
$methods = $parser->parseMethods("sayHello('Dave')");
$result = $runner->run($methods, $myClassInstance);
use Stillat\Primitives\Parser;
use Stillat\Primitives\MethodRunner;
$parser = new Parser();
$runner = new MethodRunner();
class Greeter {
public function sayHello($name)
{
return 'Hello, '.$name;
}
}
class MethodTarget
{
protected $instance;
protected $safePhpFunctions = [
'strtoupper'
];
public function __construct()
{
$this->instance = new Greeter();
}
public function __call($name, $arguments)
{
// Replace with whatever logic makes sense. This approach
// utilizes an allowed list of functions, but using
// something like function_exists also works.
if (in_array($name, $this->safePhpFunctions)) {
return call_user_func($name, ...$arguments);
}
return call_user_func([$this->instance, $name], ...$arguments);
}
}
$instance = new MethodTarget();
$result = $parser->parseMethods('sayHello(strtoupper("this is lowercase"))');
$methodResult = $runner->run($result, $instance);