the __construct () Constructor

1. constructor class is instantiated automatically when invoked,

2. The sub-class has no constructor, the constructor will directly call the culvert the parent class, inheriting the properties and methods of the parent class

3. subclass and superclass constructor has not automatically call the parent class constructor when instantiating a subclass, only to call their own subclass constructor.

   With parent :: the __construct (); may call the constructor of the superclass.

Constructor __construct ThinkPHP in __initialize () and classes ()
There are many online statement and usage of __initialize (), and always feel wrong, so he tested it. The results and share. Please do not correct.
First of all, I would say
1, __ initialize () function is not a php class, php class constructor only __construct ().
2, initialization class: If you have your own subclass constructor (__construct ()), then call their own initialized, and if not, then the parent class constructor calls its initialization.
3, when parent and child class has __construct () function of time, if you are calling __constrcut parent class while initializing a subclass of time (), you can use the parent in a subclass :: __ construct ().
If we write two classes, as follows

  1. Class Action { 
  2.     public function __construct()
  3.     {
  4.         echo 'hello Action';
  5.     }
  6. }
  7. class IndexAction extends Action{
  8.     public function __construct()
  9.     {
  10.         echo 'hello IndexAction';
  11.     }
  12. }
  13. $test = new IndexAction;
  14. //output --- hello IndexAction

Obviously initialize the subclass IndexAction time will call their constructor, so the output is 'hello IndexAction'.
But will modify the subclass

  1. classIndexActionextendsAction{   
  2.     public function __initialize()
  3.     {
  4.         echo 'hello IndexAction';
  5.     }
  6. }

Then the output is 'hello Action'. Because subclass IndexAction not have its own constructor.
If I want to setup sub-class, while the parent class constructor call it?

  1. classIndexActionextendsAction{   
  2.     publicfunction __construct() 
  3.     {
  4.         parent::__construct();
  5.         echo 'hello IndexAction';
  6.     }
  7. }

This can be simultaneously output two sentences.
Of course, there is a way for subclasses to call in the parent class.

  1. Class Action { 
  2.     publicfunction __construct() 
  3.     {
  4.         if(method_exists($this,'hello'))
  5.         {
  6.             $this -> hello();
  7.         }
  8.         echo 'hello Action';
  9.     }
  10. }
  11. class IndexAction extends Action{
  12.     public function hello()
  13.     {
  14.         echo 'hello IndexAction';
  15.     }
  16. }

Such two words may be simultaneously output.
And, in the method of this subclass hello () is similar in ThinkPHP __initialize ().
So, there ThinkPHP in __initialize () just programmers to avoid frequent use of parent when writing a subclass :: __ construct (), while the right constructor calls within the framework of the parent class, so we are in ThnikPHP subclass initialization time to use __initialize (), instead of __construct (), of course you can also modify the framework __initialize () function modifies the function name as you like.

Turn: http: //www.thinkphp.cn/code/367.html

Guess you like

Origin www.cnblogs.com/ygyy/p/11563356.html