Home  >  Article  >  Backend Development  >  [Happy 100 days of learning PHP (7)] Lecture on the PHP constructor: The grievances between father and son_PHP Tutorial

[Happy 100 days of learning PHP (7)] Lecture on the PHP constructor: The grievances between father and son_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:27907browse

Foreplay part:


1. One sentence summary php:

Php is used to make web applications, among which it is very suitable for websites. The famous wordpress is developed with php. Please do not consider using php for applications other than web.

Explanation of some keywords involved in this article:

Gaoshangda: short for high-end and high-end. The Internet is too popular, and netizens are too lazy, saying that they are all unhappy


Several versions of Php:

PHP4 should be said Milestone versions of PHP, such as the introduction of the zend engine and various major accelerations. However, this version is too weak in object orientation, so it is right to ignore php4.

PHP5 If you want to develop, please download version 5.3, which is the most widely used and relatively stable. Please delete 5.0~5.2 directly. Please use it with caution after 5.4. One is because it is not popular yet, and the other is because it is not compatible with versions before 5.4 (many open source libraries on the Internet are not compatible with versions after 5.4)

PHP6 You can download I admire you.


About PHP learning:

If you just want to learn PHP syntax quickly, please use the PHP compiler and installer under Windows. One-click installation and configuration are simple. It is not recommended to start using Linux directly to learn PHP. After you configure it correctly, you will no longer be interested in PHP.

If you have learned how to install it in a commercial environment, please be sure to learn how to configure and develop PHP under Linux. After all, many excellent third-party supports are native to Linux, and Linux is also the best in terms of efficiency and stability. Of course, PHP's support in IIS7 is said to be good, but please don't take it too seriously, it's boring.


2. Text part:

What is a constructor

Each The characteristics of constructors in languages ​​(such as java, c#, php) are somewhat different. A function that can be automatically executed when each object is initialized. This function does not need to set anything, but if you set it, it will be executed automatically. More importantly, you cannot manually call this function externally.

Generally speaking, the constructor names and class names of most languages ​​are the same. PHP is an exception here. It used to be like this, but in php5, although it can still be the same, it is no longer recommended for everyone to use it. The function name is the same as the class name.

Why should there be a constructor

if it is an independent single class. In fact, the constructor also plays a "convenient" role.

For example:

class A

{

function A()

{

//Do some initialization work here

}

}

Does this make sense? In fact, it makes no sense. You can call the things in A() externally.

So when and where does the constructor make sense? In fact, it is about objects and object inheritance. Give me a story-based example:

For example: : After a year, I finally developed a very complete set like a mad dog. I have a high-end function class library, and I think that there is no similar product in this class library in China. In order to prevent others from understanding it, I also wrote the class library structure to be extremely complicated. Then accidentally sold it to multiple programmers. But what? I really want to do some statistics, that is, I want to know who has used my class library so that I can feel good about myself, so I designed my base class library like this:

class 牛牛 //This is the name of the high-level class library

{

function 牛牛()

{

//It’s sneaky here I collected some local information of the current user and secretly sent it to my mailbox. Isn’t it very unscrupulous?

}

//The following are other advanced functions and methods that can be called externally, slightly

}

Here we assume: If the programmer If you want to use my functions, you must inherit my class library, otherwise you will not understand how to call:

Class Mavericks extends Niu Niu//Note that this is directly the inheritance writing method of PHP

{

//You can do whatever you want here. Anyway, I will collect the information before you do it, because I have already set the constructor of the parent class.

}

So everyone knows:

1. The constructor is not created for convenience

2. It is more for oriented It is used for the structural design of objects

3. Its inheritance, controllability, and systematization are the main uses of the constructor, and productization and commercialization are the main purposes of using it

4 . If you know how to develop IOS, you will see that if the view you write inherits UITableView, then your interface will automatically render the table in the end. Do you want to know how Apple implements UITableView? There is no way to do it. The only way is to inherit it honestly and write code based on mine.

5. Programming does not mean that the program is elegant, beautiful, artistic, or difficult to understand. The master has his own purpose when writing any program. Similarly, every word a smart boss says to you has a purpose, not to reflect how good his eloquence is

Characteristics of PHP's constructor

The above restrictions may seem fine, but they can actually be broken. That is, after a subclass inherits a parent class in PHP, if the subclass sets its own constructor , then the parent class's function will not be executed automatically. Look at the code:

class Mavericks extends Niu Niu//Note that this is directly the inheritance writing method of PHP

{

function Mavericks()

{

//I have my own constructor, so the parent class has less to worry about.

}

}

If I write it like this, I won’t be able to use it in the parent class. The dirty act of "collecting information" is performed in the constructor of a class.

Constructor features in PHP5

The constructor in most languages ​​​​must be the same as the class name. In fact, the above writing method is the writing method of PHP4 . In PHP5, the founder used the __construct function instead. Of course, the original writing method is still compatible with

. Let’s take a look at such an ultimate writing method (why is it called ultimate? Because it is written I was so tired that I had to stop and write it in one step)

class father

{

function __construct() //Here you can either write function father() or Just use __construct

{

echo "Father";

}

}

class child extends father

{

function child()

{

echo "son"; //If the child has its own constructor, "Father" will not be output

// parent::__construct(); // If you insist on executing father's constructor, you have to do this, there is no other way

}

}

$child=new child();

I will summarize the above constructor:

1. Both father and son are born from the same ancestor, and they are one family and inherited from the same family.

2. If the son has not started a family (he has no constructor), then the son must tell everyone that he is the son of XX, otherwise no one will know the son (The father’s constructor will be forced to execute automatically)

3. If the son grows up, he doesn’t want his father to be in charge. Then he must start a family and start a business by himself (define the constructor by himself). You can now tell the outside world who you are without telling others who your father is (Only execute your own constructor)

4. After the son started his career, he found that it was not possible and sometimes he had to rely on his father. Bring some connections to help him with projects. So we can only continue to mention his father’s name outside (Execute parent::__construct() to execute the constructor of the parent class)

Let’s follow Let’s look at the last piece of code in this chapter:

class father

{

function __construct() //Here you can either write function father() or use __construct

{

echo "Father";

$this->talk();

}

}

class child extends father

{

function child()

{

echo "son"; //If child has its own structure here function, "Father" will not be output

parent::__construct(); //If you insist on executing father's constructor, you have to do this, there is no other way

}

function talk()

{

echo "Please help my son";

}

}

$child=new child();

Look at the parent class first. You will find that initializing the parent class alone will make an error because there is no talk function in the parent class. So you can only initialize the subclass (child). If parent::__construct(); is executed, you will find that talk in the subclass is executed, but it is actually automatically executed by the constructor of the parent class.

After seeing this, you will definitely have a new view and understanding of the PHP constructor. In fact, this is really useful. I will continue the above rhythm and use the above tone to explain this code:

1. One day, my father finally became old and could no longer go out to help his son with projects, so he could only stay at home and cook. Done (It will be wrong to initialize the parent class alone)

2. The son went on a rampage relying on the resources his father had brought him, and rarely went back to visit his father. He also claimed that his father was really useless, and almost wanted to never go back to visit his father again

3 , Finally one day, the son had a big problem in his career, and only his father's friends could solve this problem.

4. However, the old father was sick and lay in bed all day. For the sake of his son, the father wrote a letter to his friend regardless of the past, and told his son that he must hand it to his friend in person before he can open it

5. The son handed the letter to his father’s friend (The constructor of the subclass enforces the constructor of the parent class)

6. The father’s friend opened the letter, The son's eyes were blurry with tears, and the letter read: Please help my son (The constructor of the parent class automatically executes the method of the subclass)

Okay, the PHP constructor is pretty much the same. Well, in fact, not only PHP, but any development language still in use has its uniqueness. No language has a future and no one has a future. It's just that the fields are different, the customer requirements are different, and the money is different.


Here is another suggestion for you: Writing programs is the same as being a human being. People who are good at being, sensible, and caring must be very popular with everyone; similarly, people who can serve users Programmers who think about it will definitely write code that will be liked by users, leaders, and colleagues.

Quote from Stephen Chow in the movie "The God of Cookery": What does it mean to "make the best dishes with your heart"? This heart is not in a specific dish, this heart is here, in life. , in your understanding and perception of the world.


-------------------------- -

Follow my WeChat official account and become a little more mature every day
Following steps:
Take out your phone very directly -> Open WeChat - >Click Scan and scan the QR code below

Enter php1, php2, php3, php4 to view the lecture in WeChat

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735869.htmlTechArticleForeplay part: 1. Summarize php in one sentence: Php is used to make web applications, among which this one is very Suitable for websites, the famous WordPress is developed by PHP. Please do not use applications other than the Web...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn