在PHP中定义类方法 [英] Defining class methods in PHP

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

问题描述

是否可以在 PHP(因为它在C ++中)声明一个类方法 OUTSIDE < c $ c>类定义

Is it possible in PHP (as it is in C++) to declare a class method OUTSIDE the class definition?

推荐答案

但是,您可以使用 __ call 魔术方法将调用转发到仲裁函数或方法。

No, as of PHP 5.2. However, you may use __call magic method to forward call to arbitraty function or method.

class A {

    public function __call($method, $args) {
        if ($method == 'foo') {
            return call_user_func_array('bar', $args);
        }
    }

}

function bar($x) {
    echo $x;
}

$a = new A();
$a->foo('12345'); // will result in calling bar('12345')

可能)支持 traits 。 Trait是不能被实例化为独立对象的方法的实现。相反,trait可以用于扩展包含实现的类。详细了解Traits 此处

In incoming PHP 6 there will be (most likely) support for traits. Trait is an implementation of method(s) that cannot be instantiated as standalone object. Instead, trait can be used to extend class with contained implementation. Learn more on Traits here.

这篇关于在PHP中定义类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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