Class construction methods and destructor methods and encapsulation of the purpose and encapsulation of the procedure + access modifiers

Source: Internet
Author: User

I. Encapsulation purpose: In order to make the class more secure.

Encapsulation Practices:

The member variable inside the ① class is private

② using member methods to access member variables indirectly

③ in this method, add the restriction condition

Note: The same name method is not allowed in the PHP class.

Two. Access modifiers

①public: Public-speaking, accessible anywhere.

②protected: Represents a protected, accessible only in that class or subclass of that class.

③private: Represents a private, accessible only in this class.

Three. Construction method Considerations for classes

The ① constructor method name can be the same as the class, you can use __construct (), and the latter is advocated.

The ② constructor method does not return a value.

The main function of ③ is to complete the initialization of the new object, not to create the object itself.

④ after the object is created, the system automatically calls the constructor of the class.

⑤ a class has and only one constructor method.

⑥ If you do not give the Class A custom construction method, the class uses the system default construction method.

⑦ if a constructor is customized for a class, the default constructor method for that class is overridden.

The default access modifier for the ⑧ constructed method is public.

Four. Destruction methods

Examples

<?PHPclassperson{ Public $name;  Public $age;  PublicFunction__construct ($name,$age){    $this->name=$name; $this->age=$age;} function__destruct () {//This is the destructor method        Echo $this->name. " Destroy Resources <br/> ";}}$p 1=NewPerson ("Jia Baoyu", 16);$p 2=NewPerson ("Lin Daiyu", 14);?>

Through the above examples, we can conclude:

1. The destructor method is called automatically.

2. The destructor is mainly used for destroying resources.

3. The destructor invocation order is that the object that was created first is destroyed.

4. When the destructor method is called:

① when the program (process end) exits.

② when an object is called a garbage object, the object's destructor is also called.

③ is the so-called garbage object, which means that there is no variable to reference it.

④ once an object becomes a garbage object, the destructor is called immediately.

Considerations for the destructor method of the class

①php5 addition of the destructor function __destruct ()

The ② destructor method has no return value.

③ The main function is to release the operation of the resource, not destroying the object itself.

④ The class's destructor is automatically called before the object is destroyed.

⑤ A class has a maximum of one destructor method.

Class construction methods and destructor methods and encapsulation of the purpose and encapsulation of the procedure + access modifiers

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.