Php object property Assignment loss problem?

Source: Internet
Author: User

  
   name = 'zhangsan';    }    public function test2()    {        print_r($this->name);    }}$obj = new demo();$obj->test1();$obj->test2(); // 输出 zhangsan我在一个PHP文件里面执行结果如上,但是我用框架开发网站测试调用的时候执行流程如下:1.我的test1方法跳到模板 ,并且方法里将$name 赋值了 'lisi'2.模板提交的时候提交到了test2,并且打印出属性$name为空这是为啥呢??

Reply content:


  
   name = 'zhangsan';    }    public function test2()    {        print_r($this->name);    }}$obj = new demo();$obj->test1();$obj->test2(); // 输出 zhangsan我在一个PHP文件里面执行结果如上,但是我用框架开发网站测试调用的时候执行流程如下:1.我的test1方法跳到模板 ,并且方法里将$name 赋值了 'lisi'2.模板提交的时候提交到了test2,并且打印出属性$name为空这是为啥呢??

Think of several possible

1. Not the same instance

Are the methods of referencing objects really referenced on the same instance?

2. There are magic ways to change the behavior

php
  
   {self::confuse($name)} = $value;    }    private static function confuse($name) {        return substr($name, 0, 4) !== 'bad_' ? "bad_$name" : $name;    }}$o = new BadAss;$o->name = 'value';var_dump($o);//bad_name => value

Is it an entity?

You want to get $name directly using $obj->name not to get it.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.