doumao1047 2012-09-24 11:23
浏览 38
已采纳

为什么netbeans和PHP都没有报告此代码的错误

Why do neither netbeans nor PHP report an error from this code:

public function __construct ()
{
global $blog;
$this->_blog_id = $blog->blog_id;
$this->_post_amount = $blog->
$this->_limit_per_page = $blog->config_get('posts_limit_per_page');
$this->_short_sign_limit = $blog->config_get('posts_short_sign_limit');
}

I had a phone call and forgot about that unfinished 3rd line, saved my work and website silently died on it.

  • 写回答

2条回答 默认 最新

  • dqyym3667 2012-09-24 11:24
    关注
    $this->_post_amount = $blog->
    $this->_limit_per_page = $blog->config_get('posts_limit_per_page');
    

    can also be written as

    $this->_post_amount = $blog->$this->_limit_per_page = $blog->config_get('posts_limit_per_page');
    

    which makes no sense but is perfectly valid.

    However, in your case it breaks your script because using $instance->$other_instance without a __toString method results in this error: Object of class Test could not be converted to string. You IDE does not check for this since since it's really an edge case and as soon as it's not $this->$this but e.g. $this->$that with $that being e.g. the return value of another function it would be near impossible to know what $that can be.


    Here's some example code that proves how $this->$this can actually work fine:

    <?php
    class Foo {
        public $foo = 'bar';
    }
    
    class Test {
        private $xyz;
        function __construct() {
            $this->xyz = new Foo();
        }
        function __toString() {
            return 'xyz';
        }
        function run() {
            echo $this->$this->foo;
        }
    }
    
    $t = new Test();
    $t->run();
    

    The $this->$this statement will result in __toString being used for the second $this and thus it will be equivalent to $this->xyz so the whole line will end up as echo $this->xyz->foo; which is valid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭