Why does this print out 33 instead of 11?

Source: Internet
Author: User
If you remove the constructor, print 11 without removing it, then print 33. Public $name = ' 11 '; Will this sentence not be executed?


  
   name=33;   } }$a=new man();echo $a->name;?>

Reply content:

If you remove the constructor, print 11 without removing it, then print 33. Public $name = ' 11 '; Will this sentence not be executed?


  
   name=33;   } }$a=new man();echo $a->name;?>

The declaration or initialization of a variable should be executed first, and the constructor will be assigned a value.

Isn't this the most basic grammar?

Constructors First Run

Initialize the variables first,

The constructor is then executed.

The value of $name when initialized is changed in the constructor

class man{    public $name='11';    public function __construct(){        echo $this->name;        $this->name=33;    }}$a=new man();echo $a->name;

?>

Results: 1133

1, when you new Man (), the construction method is executed, so the value of the $this->name property inside will be changed

The man class has a constructor that executes every time new object is executed, and the value of $name becomes 33 after execution.

  • 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.