Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of different initialize in Constructor construct and ThinkPHP

Shulou Source: shulou.com Published: 2022-06-03 04:04:24 10月03日 Update

This article shares with you the analysis of different examples of initialize in the constructors construct and ThinkPHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Both the initialize () and construct () functions in ThinkPHP can be understood as constructors, the first is unique to the tp framework, and the latter is the php constructor, so what's the difference between the two?

Searching on the Internet, many of the answers are the same. It is wrong to say that initialize in ThinkPHP is equivalent to construct of php. If so, why does tp not use construct and make its own initialize constructor for ThinkPHP?

Try it yourself and you'll know the difference between the two.

A.php

Class a {

Function _ construct () {

Echo 'a'

}

}

Copy the code

B.php (Note: here the constructor does not call parent::__construct ();)

Include 'a.php'

Class b extends a {

Function _ construct () {

Echo 'b'

}

}

$test=new b ()

Copy the code

Running result:

B

Copy the code

It can be seen that although class b inherits class a, the output proves that the program only executes the constructor of class b, not the constructor of the parent class automatically.

If you add parent::__construct () to the constructor of b.php, it's different.

Include 'a.php'

Class b extends a {

Function _ construct () {

Parent::__construct ()

Echo 'b'

}

}

$test=new b ()

Copy the code

Then the output is:

Ab

Copy the code

The constructor of the parent class is executed at this point.

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

BaseAction.class.php

Class BaseAction extends Action {

Public function _ initialize () {

Echo 'baseAction'

}

Copy the code

IndexAction.class.php

Class IndexAction extends BaseAction {

Public function () {

Echo 'indexAction'

}

Copy the code

Run the index method under Index to output the result:

BaseActionindexAcition

Copy the code

As you can see, the _ initialize method of the subclass automatically calls the _ initialize method of the parent class. And the constructor construct of php, if you want to call the method of the parent class, you must display the call parent::__construct () in the constructor of the subclass.

This is the difference between initialize and construct in ThinkPHP.

Thank you for reading! This is the end of this article on "example analysis of different initialize in constructor construct and ThinkPHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Function code different method result output example analysis two content subclass more article run good practical no just just article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Microsoft OPPO Reno macOS