欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

php 5 __toString()方法 类转换为字符串,__tostring字符串

来源: javaer 分享于  点击 23064 次 点评:153

php 5 __toString()方法 类转换为字符串,__tostring字符串


<?php
    class TestClass
    {
        private $a;
        private $b;
        private $c;

        public function __construct($a, $b, $c){
            $this->a = $a;
            $this->b = $b;
            $this->c = $c;
        }

        public function __toString(){
            return $this->a.' '.$this->b.' '.$this->c;
        }
    }

    $class = new TestClass('Good', 'morning,', 'Sir!');
    echo $class;
?>

/*
在php5中,可以为类实现一个名为__toString()的方法以将类转换为一个字符串。
当尝试打印实现了该方法的类的实例时,__toString()方法被自动调用。
在php 5.2.0之前,__toString()只在实例直接被echo() 和 print()输出时被调用
*/

相关文章

    暂无相关文章

用户点评