<?php
class Animal {
public function eat() { echo 'Yummy!'; }
}
class Plant {
public $size = 0;
public function grow() { echo 'I\'m level '.++$this->size; }
}
if (rand(0, 1) == 0) $base = 'Animal'; else $base = 'Plant';
class_alias($base, "LifeformBase");
class Lifeform extends LifeformBase { }
var_dump(new Lifeform);
// similar for ReflectionClass, you just need to fetch the name
// inheritance by anonymous class; also requires an object representing the anonymous class, like Closure is for anonymous functions.
function get_base_animal_class() {
return new class extends Animal {
public $color;
public function hiss() { echo 'Hiss!'; }
};
}
class_alias(get_class(get_base_animal_class()), "AnonBase");
$tiger_class = new class extends AnonBase {
public $color = 'orange';
};
var_dump($tiger_class->hiss(), $tiger_class->color);
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/SKWdW on line 23
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_CLASS in /in/SKWdW on line 23
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/SKWdW on line 23
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/SKWdW on line 23
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/SKWdW on line 4
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/SKWdW on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/SKWdW on line 4
Process exited with code 255.