PhpSandbox\PHPSandbox::defineClass PHP Méthode

defineClass() public méthode

You can pass the class $name and $value to define, or an associative array of classes to define
public defineClass ( string | array $name, mixed $value )
$name string | array String of class $name or associative array to define
$value mixed Value to define class to
    public function defineClass($name, $value)
    {
        if (is_array($name)) {
            return $this->defineClasses($name);
        }
        if (!$name) {
            $this->validationError("Cannot define unnamed class!", Error::DEFINE_CLASS_ERROR, null, '');
        }
        $name = $this->normalizeClass($name);
        $this->definitions['classes'][$name] = $value;
        return $this;
    }

Usage Example

    public function testStaticTypeOverwriting()
    {
        $class = 'B' . md5(time());
        $this->sandbox->defineClass('A', $class);
        $this->sandbox->allow_classes = true;
        $this->sandbox->allow_functions = true;
        $this->assertEquals("Yes", $this->sandbox->execute('<?php
                class ' . $class . ' {
                    public $value = "Yes";
                }

                function test' . $class . '(A $var){
                    return $var->value;
                }

                return test' . $class . '(new ' . $class . ');
            ?>'));
    }
PHPSandbox