Difference between initialize and construct in ThinkPHP

Source: Internet
Author: User
The initialize () and construct () functions in ThinkPHP can be understood as constructor. the previous one is unique to the tp framework, followed by the php constructor, so what are the differences between the two? The initialize () and construct () functions in ThinkPHP can be understood as constructor. the previous one is unique to the tp framework, followed by the php constructor, so what are the differences between the two?

In online search, many answers are the same. initialize in ThinkPHP is equivalent to construct in php. this is wrong. if so, why does tp not use construct, what if we need to get a ThinkPHP version of initialize constructor?

Try it and you will know the differences between the two.

A. php
  1. Class {
  2. Function _ construct (){
  3. Echo 'A ';
  4. }
  5. }
Copy the code B. php (note: The constructor does not call parent ::__ construct ();)
  1. Include 'A. php ';
  2. Class B extends {
  3. Function _ construct (){
  4. Echo 'B ';
  5. }
  6. }
  7.  
  8. $ Test = new B ();
Copy the code running result:
  1. B
The copied code shows that although Class B inherits Class a, the output shows that the program only executes Class B constructor, but does not automatically execute the constructor of the parent class.

If the constructor of B. php is added with parent :__ construct (), it is different.
  1. Include 'A. php ';
  2. Class B extends {
  3. Function _ construct (){
  4. Parent: :__ construct ();
  5. Echo 'B ';
  6. }
  7. }
  8.  
  9. $ Test = new B ();
Copy the code and the output result is:
  1. AB
The constructor of the parent class is executed only when the code is copied.

Let's take a look at the initialize () function of thinkphp.

BaseAction. class. php
  1. Class BaseAction extends Action {
  2. Public function _ initialize (){
  3. Echo 'baseaction ';
  4. }
Copy the code IndexAction. class. php
  1. Class IndexAction extends BaseAction {
  2. Public function (){
  3. Echo 'indexaction ';
  4. }
Copy the code and run the Index method under index. the output result is as follows:
  1. BaseActionindexAcition
Copy the code. the _ initialize method of the subclass automatically calls the _ initialize method of the parent class. Construct of php, if you want to call the method of the parent class, you must call parent :__ construct () in the subclass construct display ();

This is the difference between initialize and construct in ThinkPHP.

This article talks about blog originality. For more information, see the source!
Author: Xiaotan blog
Web: http://tanteng.sinaapp.com/2013/04/thinkphp-initialize-construct/

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.