PHP access class inside another class

2019-02-17 04:00发布

So I have two classes like this:

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

I want to access the methods of foo inside all methods bar, without declaring it in each method inside bar, like this:

class bar {
    public function bar () {
        global $foo;
        echo $foo->something();
    }
    public function barMethod () {
        global $foo;
        echo $foo->somethingElse();
    }
    /* etc */
}

I don't want to extend it, either. I tried using the var keyword, but it didn't seem to work. What do I do in order to access the other class "foo" inside all methods of bar?

5条回答
一纸荒年 Trace。
2楼-- · 2019-02-17 04:21

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.

查看更多
再贱就再见
3楼-- · 2019-02-17 04:21

An option is to autoload your classes. Also, if you make your class a static class, you can call it without $classname = new classname():

//Autoloader
spl_autoload_register(function ($class) {
$classDir = '/_class/';
$classExt = '.php';
include $_SERVER['DOCUMENT_ROOT'] . $classDir . $class . $classExt;
});

//Your code
class bar {
    private static $foo = null; //to store the class instance

    public function __construct(){
        self::$foo = new foo(); //stores an instance of Foo into the class' property
    }

    public function bar () {
        echo self::$foo->something();
    }
}

If you convert your class (foo) into a static class

//Autoloader
spl_autoload_register(function ($class) {
$classDir = '/_class/';
$classExt = '.php';
include $_SERVER['DOCUMENT_ROOT'] . $classDir . $class . $classExt;
});

//Your code
    class bar {
        public function bar () {
            echo foo::something();
        }
    }
查看更多
一夜七次
4楼-- · 2019-02-17 04:22

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();
    }
}
查看更多
一夜七次
5楼-- · 2019-02-17 04:30

You could do like this too:

class bar {
    private $foo = null;

    function __construct($foo_instance) {
      $this->foo = $foo_instance;
    }

    public function bar () {
        echo $this->foo->something();
    }
    public function barMethod () {
        echo $this->foo->somethingElse();
    }
    /* etc, etc. */
}

Later you could do:

$foo = new foo();
$bar = new bar($foo);
查看更多
虎瘦雄心在
6楼-- · 2019-02-17 04:36

And if your only focus is the methods themselves as opposed to the instance of another class, you can use x extends y.

class foo {
  function fooMethod(){
    echo 'i am foo';
  }
}

class bar extends foo {
  function barMethod(){
    echo 'i am bar';
  }
}

$instance = new bar;
$instance->fooMethod();
查看更多
登录 后发表回答