zoukankan      html  css  js  c++  java
  • php __tostring 与 tostring

    简介:这是php __tostring 与 tostring的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

    class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340633' scrolling='no'>
    转自:

    转自:单党育的BLOG

    function __toString()
        {
            return $this->content;
        }
    //输出字符串
        function toString()
        {
            return $this->content;
        }
    内容是一样,不知道前面那两个特殊的下划线有什么意义,是同一个类中的两个方法?
    回答:
    执行的结果相同. 区别在于,
    前一个是魔术函数, 在需要字符串值的地方会自动调用它进行对象的类型转换.
    后一个需要在代码中明确调用才有机会执行.
    实例
    class MyClass
    {
        public function __toString()
        {
            return 'call __toString()';
        }
        public function toString()
        {
            return 'call toString()';
        }
    }

    $my new MyClass();
    echo $my '<br />'; //自动调用(隐式)__toString转成string
    echo $my->toString() '<br />'; //调用(显式)toString去转成string
    echo $my->__toString() '<br />'; //如果这样调用, 代码会不好看
    echo (string)$my '<br />';
    __toString()会在需要转成字符串时, 会隐式自动调用它, 在PHP内部.  这个也是来自JAVA的. 建议在__toString()中调用toString(), 这样就不会代码重复了.

    爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

    http://biancheng.dnbcw.info/php/340633.html pageNo:7
  • 相关阅读:
    中国用户mac上快速安装nodejs
    移动前端的开发痛点
    前端模块化思考
    tcpdump学习
    curl 同时发送多个请求
    apache 做http代理
    文件 FIFO队列
    mysql多表字段名重复的情况
    时间戳实现增量数据同步
    python 学习
  • 原文地址:https://www.cnblogs.com/ooooo/p/2245225.html
Copyright © 2011-2022 走看看