PHP 5.0 Examples of constructors explain _php tutorial

Source: Internet
Author: User
Everyone is right.If you declare a function in a class named __construct, the function will be treated as a constructor and executed when an object instance is created. Clearly, __ is an underscore of two. Just like any other function, the PHP 5.0 constructor may have parameters or default values. You can define a class to create an object and put its properties all in one statement (statement).

You can also define a function called __destruct, and PHP will call this function before the object is destroyed. It is called a destructor.

Inheritance is a powerful feature of a class. A class (subclass/derived class) can inherit the functionality of another class (parent/base class). The derived class will contain all the properties and methods of the base class, and other properties and methods can be added to the derived class. You can also override the base class's methods and properties. As shown in the previous article, you can use the extends keyword to inherit a class.

You might want to know how the PHP 5.0 constructor is inherited. When they are inherited along with other methods, they are not executed when the object is created.

If you need this feature, you need to use:: operator. It allows you to point to a namespace. The parent points to the parental namespace, and you can use Parent::__construct to invoke the constructor of the parent class.

Some object-oriented languages name constructors after the class. This is true for the first few versions of PHP, which is still valid. That is, if you name a class animal and you create a name in it that is also animal, this method is the constructor. If a class has __ Construt constructor and the same function as the class name, PHP will think of __construct as a constructor function. This makes the classes written in the previous PHP version still available. However, the new script (PHP5) should use __construct.

This new method of declaring constructors in PHP allows the PHP 5.0 constructor to have a unique name, regardless of the name of the class in which it resides. When you change the name of a class, you do not need to change the name of the constructor.

You might give constructors a way to access the constructor in PHP just like any other class method. Access will affect the ability to instantiate objects from within a certain range. This allows the implementation of some fixed design patterns, such as the singleton mode.

destructor, as opposed to a constructor function. PHP calls them to destroy an object from memory. By default, PHP simply frees the memory occupied by the object's properties and destroys the object-related resources. Destructors allow you to execute arbitrary code after an object is used to clear memory.

When PHP decides that your script is no longer related to the object, the destructor is called. Within the namespace of a function, this occurs at the time of the function return. For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable that points to the object. Variables are usually assigned on duty to null or called unset.

In the following example, the number of objects instantiated from a class is computed. The counter class starts adding value from the PHP 5.0 constructor, reducing the value in destructors.

 
 
  1. Class Counter
  2. {
  3. private static $ Count = 0 ;
  4. function __construct ()
  5. {
  6. Self:: $count + +;
  7. }
  8. function __destruct ()
  9. {
  10. Self:: $count--;
  11. }
  12. function GetCount ()
  13. {
  14. Return self:: $count;
  15. }
  16. }
  17. Establish the first instance
  18. $ C = New Counter ();
  19. Output 1
  20. Print ($c->getcount (). "N");
  21. Create a second instance
  22. $ C2 = New Counter ();
  23. Output 2
  24. Print ($c->getcount (). "N");
  25. Destroying instances
  26. $ C2 = NULL ;
  27. Output 1
  28. Print ($c->getcount (). "N");
  29. ? >

Once you have defined a class, you can use new to create an instance of the class. The definition of a class is a design diagram, and an instance is a component placed on an assembly line. New requires the name of the class and returns an instance of the class. If the PHP 5.0 constructor requires parameters, you should enter the parameters after new.


http://www.bkjia.com/PHPjc/446433.html www.bkjia.com true http://www.bkjia.com/PHPjc/446433.html techarticle everyone is right. If you declare a function in a class named __construct, the function will be treated as a constructor and executed when an object instance is created. To say clearly ...

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