PHP : instantiation of an object as a property of a class -


i have line of codes:

class foo{     public $object = new bar(2);      public function index(){         dd($this->object);     } } 

and bar object contains:

class bar{     protected $number;      function __construct($number){         $this->number = $number;     } } 

now throwing me constant expression contains invalid operation

it's not possible instantiate object during class properties declaration. should done in object constructor instead :

class foo{     public $object;      public function __construct() {         $this->object = new bar(2);     }      public function index(){         dd($this->object);     } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -