PHP Object attribute assignment loss problem?

Source: Internet
Author: User
{Code ...}

  Name = 'hangsan';} public function test2 () {print_r ($ this-> name) ;}}$ obj = new demo (); $ obj-> test1 (); $ obj-> test2 (); // output zhangsan. The execution result in a PHP file is as follows: 1. my test1 method jumps to the template, and the $ name value in the method is 'lisi' 2. when the template is submitted, it is submitted to test2 and the $ name attribute is printed. Why ??

Reply content:

  Name = 'hangsan';} public function test2 () {print_r ($ this-> name) ;}}$ obj = new demo (); $ obj-> test1 (); $ obj-> test2 (); // output zhangsan. The execution result in a PHP file is as follows: 1. my test1 method jumps to the template, and the $ name value in the method is 'lisi' 2. when the template is submitted, it is submitted to test2 and the $ name attribute is printed. Why ??

Several possibilities

1. Not the same instance

Does the method of referencing objects before and after actually reference objects to the same instance?

2. There are magic ways to change 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 can get $ name directly using $ obj-> name.

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.