php 5.1.6 魔法 __toString 方法

发布于 2024-09-02 19:27:26 字数 541 浏览 4 评论 0原文

在 codeigniter 中,我尝试使用 this 插件,它需要我实现一个 toString我的模型中的方法。我的 toString 方法只是

public function __toString()
{
    return (string)$this->name;
}

在我的本地计算机上使用 php 5.3 一切正常,但在使用 php 5.1.6 的生产服务器上它显示“Object id#48”,其中应该出现该对象的 name 属性的值....我发现了一些关于 这里有问题,但我仍然不明白......我该如何解决这个问题?

In codeigniter Im trying to use this plugin which requires I implement a toString method in my models. My toString method simply does

public function __toString()
{
    return (string)$this->name;
}

On my local machine with php 5.3 everything works just fine but on the production server with php 5.1.6 it shows "Object id#48" where the value of the name property of that object should appear..... I found something about the problem here but I still dont understand... How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾旎 2024-09-09 19:27:26
class YourClass 
{
    public function __toString()
    {
        return $this->name;
    }
}

PHP 5.2.0

$yourObject = new YourClass();
echo $yourObject; // this works
printf("%s", $yourObject); // this does not call __toString()
echo 'Hello ' . $yourObject; // this does not call __toString()
echo 'Hello ' . $yourObject->__toString(); // this works
echo (string)$yourObject; // this does not call __toString()

PHP >= 5.2.0

$yourObject = new YourClass();
echo $yourObject; // this works
printf("%s", $yourObject); // this works
echo 'Hello ' . $yourObject; // this works
echo 'Hello ' . $yourObject->__toString(); // this works
echo (string)$yourObject; // this works
class YourClass 
{
    public function __toString()
    {
        return $this->name;
    }
}

PHP < 5.2.0

$yourObject = new YourClass();
echo $yourObject; // this works
printf("%s", $yourObject); // this does not call __toString()
echo 'Hello ' . $yourObject; // this does not call __toString()
echo 'Hello ' . $yourObject->__toString(); // this works
echo (string)$yourObject; // this does not call __toString()

PHP >= 5.2.0

$yourObject = new YourClass();
echo $yourObject; // this works
printf("%s", $yourObject); // this works
echo 'Hello ' . $yourObject; // this works
echo 'Hello ' . $yourObject->__toString(); // this works
echo (string)$yourObject; // this works
若有似无的小暗淡 2024-09-09 19:27:26

引用手册:

值得注意的是,在PHP之前
5.2.0 直接组合时才调用__toString方法
与 echo() 或 print() 一起使用。自从 PHP
5.2.0,它在任何字符串上下文中调用(例如在带有 %s 的 printf() 中)
修饰符)但不适用于其他类型
上下文(例如使用 %d 修饰符)。
从 PHP 5.2.0 开始,转换对象
没有 __toString 方法来字符串
会导致 E_RECOVERABLE_ERROR。

我认为如果您在 PHP 中使用 __toString 方法,您必须手动调用它5.2 并且不在回显或打印的上下文中。

To quote from the manual:

It is worth noting that before PHP
5.2.0 the __toString method was only called when it was directly combined
with echo() or print(). Since PHP
5.2.0, it is called in any string context (e.g. in printf() with %s
modifier) but not in other types
contexts (e.g. with %d modifier).
Since PHP 5.2.0, converting objects
without __toString method to string
would cause E_RECOVERABLE_ERROR.

I think you have call the __toString method manually if you're using it in PHP < 5.2 and not in the context of an echo or print.

紫瑟鸿黎 2024-09-09 19:27:26

升级 PHP

我正在处理同样的问题,我怀疑您最好的选择是将生产服务器上的 php 升级到 >= 5.2.0

将来(我目前正在艰难地学习这一点),尝试在您将部署到的同一版本上进行开发。

Upgrade PHP

I'm dealing with the same problem, I suspect your best option will be to upgrade php on the production server to >= 5.2.0

In the future (I'm currently learning this the hard way), try to develop on the same version you will deploy to.

葬﹪忆之殇 2024-09-09 19:27:26

对于版本 << 的版本,您必须显式调用 php 魔术函数 __toString() 5.2.所以你的代码将变成这样:

    public function myname()
    {
       $name = $this->name;
       return $name.__toString(); //for php versions < 5.2,will also work > 5.2
    }

For versions > 5.2 自动调用__toString

You have to explicitly call the php magic function __toString() for versions < 5.2. So your code will become something like this:

    public function myname()
    {
       $name = $this->name;
       return $name.__toString(); //for php versions < 5.2,will also work > 5.2
    }

For versions > 5.2 the __toString is automatically called

醉酒的小男人 2024-09-09 19:27:26

您需要安装sudo apt install php7.0-mbstring
需要根据您的情况更改 PHP 版本。

之后不要忘记运行 service apache2 restart

希望这会有所帮助。

You need to install sudo apt install php7.0-mbstring
Need to change the PHP version as per your.

And after this do not forget to run service apache2 restart

Hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文