PHP access class inside another class

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

    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();
    

提交回复
热议问题