So, I'm writing some code as part of a project which involves the use of anonymous classes. As part of the project's criteria, there is a need for an anonymous class to be imbedded inside another anonymous class. My code is as follows:
$foo = new class($a, $b, $c) {
public $a;
public $b;
public $c;
public function __construct($a, $b, $c) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
};
public $bar = new class($x) {
public $x;
public function __construct($x) {
$this->x = $x;
}
public function __toString() {
return $this->x;
}
};
}
As you can see from the code above, $bar
is the anonymous class inside of anonymous class $foo
. I get the following PHP error code when executing this code:
PHP Fatal error: Constant expression contains invalid operations
Any help would be appreciated. Anonymous classes are new to me (and PHP it appears).
Thanks.