Sorry but I have had this problem for months and I didn't find any answer for it. Here is my code:
<?php
require_once "B.php";
class A
{
private static B $inner = new B(); // Line 7
public static function getInstance() : B
{
return self::$inner;
}
}
?>
I get a Fatal error:
Constant expression contains invalid operations on line 7
Here are my other classes codes: B.php
<?php
class B
{
public function getInfo() : string
{
return "This is " . self::class . " class";
}
}
?>
And index.php
<?php
require_once "A.php";
echo A::getInstance()->getInfo() . PHP_EOL;
?>
Can anyone explain to me why I'm getting this error?
I want to know why PHP does not allow me to declare an object as a private static field.