While making the static class for database getting this error

后端 未结 4 2114
鱼传尺愫
鱼传尺愫 2020-12-22 10:05

Here is my class

class Databases {
    public $liveresellerdb = new Database(\'1host1\',\'user\',\'pswd\',\'db\');
}

the error i am gettin

4条回答
  •  隐瞒了意图╮
    2020-12-22 10:47

    you cannot assign object during the class preperation stages, only the class instantation:

    class Databases
    {
        public $liveresellerdb;
    
        public function __construct()
        {
            $this->liveresellerdb = new Database('1host1','user','pswd','db');
        }
    }
    

    anything within the constructor can be generic PHP code, outside the function and instead the class body has specific laws.

    if you require the database's to be static then you have to set / access them differently.

    class Databases
    {
        public static $liveresellerdb;
    }
    
    Databases::liversellerdb = new Database('1host1','user','pswd','db');
    

提交回复
热议问题