php5魔术函数__toString($ param = null)不能取args

我曾经有一个像这样定义的函数(在ubuntu 9.10下运行正常):
public function __toString( $surNameFirst = false) { 
     if ($this->givenName . $this->surname == '') return null;
     else .......
}
在我将我的机器更新到ubuntu 10.04(和php版本:5.3.2-1ubuntu4.2  )我的应用程序开始显示像这样的错误==>   致命错误:方法Application_Model_Person :: __ tostring()不能在第39行的/home/speshu/Development/where/application/models/Person.php中获取参数
Call Stack:
    0.0001     616576   1. {main}() /home/speshu/Development/where/public/index.php:0  
    0.0294    1008248   2. Zend_Application->bootstrap() /home/speshu/Development/where/public/index.php:35
    0.0294    1008328   3. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /usr/local/lib/ZendFramework-1.10.0/library/Zend/Application.php:355
    0.0294    1008328   4. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /usr/local/lib/ZendFramework-1.10.0/library/Zend/Application/Bootstrap/BootstrapAbstract.php:582
    0.0387    1991416   5. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /usr/local/lib/ZendFramework-1.10.0/library/Zend/Application/Bootstrap/BootstrapAbstract.php:618
    0.0387    1991776   6. Bootstrap->_initDoctrineCLI() /usr/local/lib/ZendFramework-1.10.0/library/Zend/Application/Bootstrap/BootstrapAbstract.php:665
    0.0387    1991856   7. Bootstrap->_initDoctrine() /home/speshu/Development/where/application/Bootstrap.php:66
    0.0406    2245200   8. Doctrine_Core::loadModels() /home/speshu/Development/where/application/Bootstrap.php:93
    
已邀请:
从版本5.3开始,__ toString魔术方法不再接受参数。 在任何情况下,你真的应该用魔术方法吗?为什么不声明toString($ whatever)呢?     
function __toString() {
    if(func_num_args()>0) {
        $surNameFirst=func_get_arg(0);
    } else {
        $surNameFirst=false;
    }
....
这就是我解决这个问题的方法,它又脏又丑......但是在找到更永久的解决方案之前,它确实让系统再次运行。 您应该能够弄清楚如何最好地扩展它以满足您的需求,请注意,如果使用此方法,最好传入一个assosiative数组,因为它更容易访问数据。     
我认为调用$ obj - > __ toString()是非常难看的。 最好只调用echo $ obj;如果你不想传递参数并调用$ obj-> toString($ param);如果你想指定参数。     
另一个可能的工作。
class test
{
    var $toStringArgs = array();

    function SetToStringArgs()
    {
       $this->toStringArgs = func_get_args();
    }

    function __toString()
    {
         var_dump($this->toStringArgs);
    }
}

$test = new test();

$test->SetToStringArgs('firstname','lastname');

$test->__toString();
    

要回复问题请先登录注册