从另一个类PHP调用函数中的函数 [英] Call function in function from another class PHP

查看:207
本文介绍了从另一个类PHP调用函数中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stackoverflow上阅读了一些有关抽象类的线程,我认为这是我需要的,但我不能直接声明。

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.

想要做的是在函数1 (在类A中)中调用 function2 (在类B中)

What I want to do is to call a function2 (in classB) in a function1 (in classA).

我该如何做?

推荐答案

从ClassA访问ClassB的方法,但两者之间不需要父子关系,静态方法可能更合适:

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

这篇关于从另一个类PHP调用函数中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆