PHP access class inside another class

后端 未结 5 1112
独厮守ぢ
独厮守ぢ 2021-01-12 09:57

So I have two classes like this:

class foo {
    /* code here */
}
$foo = new foo();
class bar {
    global $foo;
    public function bar () {
        echo $         


        
5条回答
  •  一整个雨季
    2021-01-12 10:30

    Make it a member of bar. Try to never use globals.

    class bar {
        private $foo;
    
        public function __construct($foo) { $this->foo = $foo; }
    
        public function barMethod() {
            echo $this->foo->something();
        }
    }
    

提交回复
热议问题