Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of different initialize in Constructor construct and ThinkPHP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 219

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report