Function call to function from another PHP class

I read several threads about an abstract class here on Stackoverflow, and I think this is what I need, but I cannot get the declaration directly.

I want to make a call to function2 (in class B) in function1 (in class A).

How can I do it?

+4
source share
1 answer

If you only need access to the ClassB method from ClassA, but there is no parent-child relationship between them, 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(); 
+17
source

Source: https://habr.com/ru/post/1385175/


All Articles