PHP重载=运算符 [英] php overload = operator

查看:81
本文介绍了PHP重载=运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
PHP中的运算符重载

是否可以重载=运算符?

所以我想成为以下人:

class b{
    function overloadis(){
       // do somethng
    }
}

$a = new b();
$a = 'c';

在上面的示例中,当$ a ='c'时,我希望这样做;调用时,首先调用重载方法,然后该函数确定是否执行了该操作(将$ c赋给$ a)或被中止.

可以这样做吗?

提前Thnx, 鲍勃

解决方案

否. PHP不支持运算符重载,但有一些例外情况(如@NikiC所述:"PHP支持某些运算符的重载,例如[],->和(字符串),还允许重载某些语言结构(如foreach)". /p>

Possible Duplicate:
Operator Overloading in PHP

Is there a way to overload the = operator ?

So want I is the following:

class b{
    function overloadis(){
       // do somethng
    }
}

$a = new b();
$a = 'c';

In the example above, I want that when $a = 'c'; is called, the method overloadis is called first and then that function desides if the action (assign 'c' to $a) is executed or aborted.

Is it possible to do this ?

Thnx in advance, Bob

解决方案

No. PHP doesn't support operator overloading, with a few exceptions (as noted by @NikiC: "PHP supports overloading of some operators, like [], -> and (string) and also allows overloading some language constructs like foreach").

这篇关于PHP重载=运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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