1

I'm currently developing a web-based administration frontend, for an application I developed, in PHP (after several failed attempts to get Lua working with Apache and Nginx being a little weird, too) and PHP's arrays are frustrating me.

I'm trying to initialize an array of objects as I would in any other OOP-capable language:

private $leftNavItems = array(
        new LeftNavItem("./img/icon/status.png", "status", "App Status", "./articles/app_status.phtml", "app_status")
    );

however, I'm getting an error stating "expression not allowed as field default value".

Why can I not initialize an array with new objects? Would I have to work around this by using temporary variables and array_push?

EDIT : I'm coming from OOP languages such as C#, Java and C++, as well as pure procedural languages such as Lua.

SimonC
  • 1,547
  • 1
  • 19
  • 43
  • what's your LeftNavItem? – LF00 May 26 '17 at 13:33
  • It's a class with a couple variables, a constructor, and a couple getters. – SimonC May 26 '17 at 13:33
  • 1
    Possible duplicate of [Expression is not allowed as field default value](https://stackoverflow.com/questions/35037368/expression-is-not-allowed-as-field-default-value) – Schleis May 26 '17 at 13:33
  • You cannot initialize a variable with an expression, because is runtime compiled. You have to do it in constructor. Example here https://stackoverflow.com/questions/21941824/class-variable-declaration – matiaslauriti May 26 '17 at 13:34
  • 2
    Move the initialization part in the class' constructor [`__construct()`](http://php.net/manual/en/language.oop5.decon.php). Object properties can be [initialized on their definition](http://php.net/manual/en/language.oop5.properties.php) only with values that can be evaluated during compilation. – axiac May 26 '17 at 13:35
  • 1
    @Schleis I disagree, I'm asking not just for a way to work around, but also why this is a constraint. Also, I'm attempting to create an array of new objects, not initialize a value in a class. – SimonC May 26 '17 at 13:35
  • @Beatsleigher PHP doesn't let you call functions/methods when setting a value in a class. Having the keyword `private` means that your value is part of a class and not just a variable somewhere. – Schleis May 26 '17 at 13:37
  • @Beatsleigher you can do what you are trying to, but not in a property, you have to declare that array in `__construct` and you will be able to do what you want. You can't just do it before runtime. – matiaslauriti May 26 '17 at 13:37
  • @matiaslauriti That's the answer I was looking for, cheers. It's still slightly annoying, as I can easily do this in C# and I won't get any compile-time or runtime errors. – SimonC May 26 '17 at 13:40
  • @Beatsleigher yes, I know C# too and love it, but it is compiled. PHP is compiled at runtime so you can't do that because of Late Binding. – matiaslauriti May 26 '17 at 13:42
  • I guess this is one of the many reasons I despise PHP, and prefer compiled languages - and some others like Lua. Luckily I rarely have to use it. – SimonC May 26 '17 at 13:43

1 Answers1

3

Move the initialization part in the class' constructor __construct(). Object properties can be initialized on their definition only with values that can be evaluated during compilation.

class X {
    private $leftNavItems = array();

    public function __construct()
    {
        $this->leftNavItems = array(
            new LeftNavItem("./img/icon/status.png", "status", "App Status", "./articles/app_status.phtml", "app_status")
        );
    }
}

The initializers of the properties are evaluated during the compilation; they are not pieces of code to be executed when the class is initialized.

Your code attempts to initialize the variable by creating a object. Creating new objects is an activity that happens during the code execution.

axiac
  • 68,258
  • 9
  • 99
  • 134