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

查看:26
本文介绍了从另一个类 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.

我想要做的是调用function2(在classB 中)在function1 中(在classA 中).

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天全站免登陆