PHP access class inside another class

后端 未结 5 1115
独厮守ぢ
独厮守ぢ 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:20

    The short answer: nope, there is no way to implement what you want.

    Another short answer: you're working with classes in "wrong" way. Once you selected Object Oriented paradigm - forget about "global" keyword.

    The proper way of doing what you want is to create an instance of foo as member of bar and use it's methods. This is called delegation.

提交回复
热议问题