php - Explain Why This Happens Please -
i'm watching tutorial on oop php, , i've come constructor/destructor segment. when instantiate object, goes through constructor, deconstructs it.
here's code:
<?php class person { var $first_name; var $last_name; function __construct($firstname, $lastname) { $this->first_name = $firstname; $this->last_name = $lastname; echo 'hi, name is: ' . $this->first_name . ' ' . $this->last_name . '!<br>'; } function __destruct() { echo "class objects being destroyed!<br>"; } } $person1 = new person('ringo', 'starr'); $person2 = new person('john', 'lennon'); ?>
now when run code, echoes back:
hi, name is: ringo starr! hi, name is: john lennon! class objects being destroyed! class objects being destroyed!
but, logically, @ least in mind, shouldn't code echo:
hi, name is: ringo starr! class objects being destroyed! hi, name is: john lennon! class objects being destroyed!
since instantiate 1 object, goes through motions, creates new one? pretty inconsequential, want understand inner workings of programming.
thanks all.
<?php //just explaination function funccontext() {//context exists $person1 = new person('ringo', 'starr'); // person1 __constructed //"hi, name is: ringo starr!" //alive $person2 = new person('john', 'lennon'); // person2 __constructed // "hi, name is: john lennon!" // alive //they remain alive till context }//context destroyed //they destroyed //"class objects being destroyed!" //"class objects being destroyed!"
Comments
Post a Comment