In the upgraded versionIn PHP5, _ construct () is used to name the constructor, instead of the same name as the class. The advantage of this is that the constructor can be independent of the class name, when the class name changes, you do not need to modify the name of the constructor.
Opposite to constructor, in PHP5, you can define a function named _ destruct (), called PHP5 destructor, PHP will call the Destructor before the object is destroyed in the memory, so that the object can complete some work before it completely disappears. Objects can be destroyed by null.
- <?Php
- /*
- * Created on 2009-11-18
- *
- * To change the template for this generated file go
- * Window-Preferences-PHPeclipse-PHP-Code Templates
- */
- Class student {
- // Attributes
- Private $ no;
- Private $ name;
- Private $ gender;
- Private $ age;
- Private static $Count=0;
- Function _ construct ($ pname)
- {
- $ This->Name= $ Pname;
- Self: $ count ++;
- }
- Function _ destruct ()
- {
- Self: $ count --;
- }
- Static function get_count ()
- {
- Return self: $ count;
- }
- }
- $S1=NewStudent ("Tom ");
- Print (student: get_count ());
- $S2=NewStudent ("jerry ");
- Print (student: get_count ());
- $S1=NULL;
- Print (student: get_count ());
- $S2=NULL;
- Print (student: get_count ());
- ?>
The above code is the specific usage of the PHP5 destructor. I hope it will be helpful to you.