首页 期权学习期权知识正文

PHP字符串__toString()方法

xiaojiucai 期权知识 2020-08-18 514 0

toString()

  __toString()方法用于一个类被当成字符串时应怎样回应,它是快速获取对象的字符串表示的最便捷的方式,是直接输出对象引用时自动调用的方法

<?php
class TestClass
{
    public $foo;
    public function __construct($foo) 
    {
        $this->foo = $foo;
    }
    public function __toString() {
        return $this->foo;
    }
}
$class = new TestClass('Hello');
echo $class;//Hello
?>

原文链接:https://www.qiquanji.com/post/7835.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。