Php constructor and Destructor: php constructor _ PHP Tutorial

Source: Internet
Author: User
Php constructor and Destructor: php constructor. Php constructor and Destructor. php constructor is the first method automatically called after an object is created, the last php constructor and constructor automatically called before an object is released. The php constructor

Php constructor is the first method automatically called after the object is created. destructor is the last method automatically called before the object is released. This article introduces the php constructor and Destructor to coders.

Php constructor

1. it is the "first" and "automatically called" method after the object is created.
2. definition of constructor. the method name is fixed,
In php4: The same method as the class name is the constructor.
In php5: The constructor selects to use the magic method _ construct (). This name is used to declare constructor in all classes.
Advantage: you do not need to change the constructor when changing the class name.
Magic method: write a magic method in the class, and the corresponding function of this method will be added
The method names are fixed (all provided by the system) and are not defined by the user.
Every magic method is automatically called to complete a function at different times.
Different magic methods have different call times.
All methods starting _
_ Construct (); _ destruct (); _ set ();......

Purpose: initialize the member attributes;

Php destructor

1. the last method called automatically before the object is released
The garbage collector (java php) is used, and c ++ is manually released.
Purpose: close some resources for cleanup.

_ Destruct ();

Php constructor and Destructor instance

Class Person {var $ name; var $ age; var $ sex; // Constructor/* function Person () in php4 () {// Each declared object calls echo "1111111111111111";} * // The constructor function _ construct ($ name, $ age, $ sex) in php5) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {// $ this-> name; // use $ this echo for access by members in the object "my name: {$ this-> name}, my age: {$ this-> age}
"} Function run () {} function eat () {} // destructor function _ destruct () {}}$ p1 = new Person (" zhangsan ", 25, "Male"); $ p2 = new Person; $ p3 = new Person;

Address: http://www.manongjc.com/article/730.html

Related reading:

Simple use of php destructor

Simple example of php initialization object constructor and Destructor

Php constructor, destructor, and this keyword

Php constructor and Destructor analysis

The except php constructor is the first method automatically called after the object is created. The Destructor is the last automatic adjustment before the object is released...

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.