Call function in function from another class PHP

蓝咒 提交于 2019-12-03 14:55:18

问题


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 call a function2 (in classB) in a function1 (in classA).

How should I do this?


回答1:


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


来源:https://stackoverflow.com/questions/8420431/call-function-in-function-from-another-class-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!