【问题标题】:PHP: Call a method on a returned classPHP:在返回的类上调用方法
【发布时间】:2011-01-18 05:07:21
【问题描述】:

我有一个方法,它返回一个类并想在它上面调用一个方法。而不是

$theClass = $this->getClass();
$theClass->foo();

我想写

$this->getClass()->foo();

从 PHP4 开始有这种语法吗?

这行得通:

$this->{$this->getClassName()}->foo();

但我想事先操作类(我现在在 getClass() 中执行此操作)。

【问题讨论】:

    标签: php dry php4 syntactic-sugar simplification


    【解决方案1】:

    不,它不会在 PHP4 中工作,这个功能在 PHP5 中得到了改进。我想知道你为什么还在使用 PHP4 :)

    【讨论】:

    • @Carson Myers:是的,人们仍在使用 php4 很奇怪 :(
    • 我也很难过,但我的公司仍然使用 PHP4(和 Apache 1.3 - 和 Kernel 2.4)。 ;-) 也许其他人可以想出一些奇特的结构,让我可以在一行中写下我的陈述?
    • 你最好把它写成两行。每行只做一件事。
    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 2020-12-05
    • 2012-06-24
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多