0
votes

I am getting the following error!

Fatal error: Constant expression contains invalid operations in >/PATH/initClass.php on line 5

For the code:

<?php
Class init
{
    public const THEME = "aman/dev/frontend/";
    private $root = dirname(__dir__)."/aman/dev/fontend/";
    public function getFile($name,$value)
    {
        list(
            $title
            ) = $value;


    }
}
?>

I can't seem to figure out what's is happening.

Help would be appreciated.

1
I believe your issue is the use of "__dir__" in the expression for a private variable. I haven't dived deep into this, but I expect that since it can't know __dir__ until runtime, that it doesn't want to use it that way. Try setting the private variable to null, then set it with the full expression in a constructor.UncaAlby

1 Answers

2
votes

Your problem is that you are using a function operation to set a value to a class variable. To fix your problem, use the following code (i.e. move initialization to the constructor)

<?php
Class init
{
    public const THEME = "aman/dev/frontend/";
    private $root;

    public function __construct() {
        $this->root =  dirname(__dir__)."/aman/dev/fontend/";
    }

    public function getFile($name,$value)
    {
        list(
            $title
            ) = $value;


    }
}
?>