Call function in function from another class PHP

前端 未结 1 1178
猫巷女王i
猫巷女王i 2021-02-06 16:59

I have read a few threads about abstract class here at Stackoverflow and I think it\'s what I need, but I can\'t get the declaration straight.

What I want to do is to ca

1条回答
  •  名媛妹妹
    2021-02-06 17:10

    If you only need to access ClassB's method from ClassA but don't need a parent-child relationship between the two, a static method may be more appropriate:

    class ClassA
    {
      public function method1() {
        echo ClassB::method2();
      }
    }
    
    class ClassB
    {
      public static function method2() {
        return 'WOOT!';
      }
    }
    
    $cls_a = new ClassA();
    $cls_a->method1();
    
    // or alternatively, you don't even need to instantiate ClassA
    echo ClassB::method2();
    

    0 讨论(0)
提交回复
热议问题