Happy to learn PHP100 days (7)] excellent PHP constructor: The kindness and resentment of my father and son

Source: Internet
Author: User
: This article mainly introduces happy learning PHP100 days (7)] excellent PHP constructor: my father and son complained. if you are interested in PHP tutorials, please refer to it. Foreplay part:

I. Summary of php in one sentence:

Php is used for web applications, which is suitable for websites. the famous wordpress is developed by php. Do not use php for applications other than the Web.

Some key words involved in this article:

High: Abbreviation of high-end atmosphere. Internet is too busy, and netizens are too lazy to say that they are all unhappy.

Php versions:

PHP4 should be a milestone version of PHP, such as the introduction of zend Engine and various high-speed acceleration. However, this version is too weak in object orientation, and it is right to ignore php4.

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

You can download PHP6 and I admire you.

Learning about PHP:

If you only want to quickly learn the php syntax, use php under windows to compile and install the program. one-click installation and configuration is simple. We do not recommend that you learn php directly from linux. after the configuration is correct, you are no longer interested in php.

If you have learned how to install it into a commercial environment, you must learn how to configure and develop php in linux. After all, many excellent third-party support is native in linux, and efficiency and stability are also the best in linux. Of course PHP supports IIS7, which is also said to be good, but do not take it seriously.

II. text:

What is constructor?

Each language (such as java, c #, php) constructor has different features. A function automatically executed during initialization of each object. This function can be executed automatically without any content. More importantly, you cannot manually call this function externally.

Generally speaking, the constructor names and class names in most languages are the same. here, PHP is a column, and so is it, but it can be the same in php5, however, we do not recommend that you use the same function name as the class name.

Why is there a constructor?

If it is an independent single class. In fact, constructors also play a "convenient" role.

For example:

Class

{

Function ()

{

// Perform initialization here

}

}

Does this make sense? In fact, it doesn't make sense. you can put all the things in A () for external calls.

So when and where does the constructor make sense? In fact, in terms of object and object inheritance, let's give a story-based column:

For example, after one year, I finally developed a very big function library, and thought there was no similar product in China. in order to prevent others from understanding it, I also wrote extremely complex class library structures. Then I accidentally sold it to multiple programmers. But what? I really want to make some statistics, that is, I want to know who has used my class library, so that I feel very good about myself. so I designed my Base Class Library as follows:

Class Niu // This is the name of a high-class database

{

Function Niu ()

{

// Some local information of the current user is secretly collected and sent to my mailbox. Is there any concern?

}

// The following are the upper and upper functions and methods that can be called by other external entities.

}

Here we assume that if programmers want to use my functions, they must inherit the class library. Otherwise, you cannot understand how to call them:

Class calf extends Niu // note that this is the inheritance of php.

{

// Do whatever you want. I will collect information before you start, because I have set the constructor for the parent class.

}

So everyone knows:

1. constructor is not produced for convenience

2. more is used for object-oriented structure design

3. its inheritance, controllability, and systemization are the main purposes of Constructors. productization and commercialization are the main purposes of using constructors.

4. if you develop a view on IOS, you will see that if you write a view that inherits the UITableView, your interface will automatically render the table. Do you want to know how Apple implements UITableView? There is no such thing as an honest Inheritance. it is the King to write code on my own.

5. programming is not to say that writing is elegant, elegant, artistic, or ugly. The Master has his own purpose when writing any program. Similarly, a boss with a wrist has a purpose to say everything to you, not to reflect how good his eloquence is.

PHP constructor features

The above restrictions seem okay, but they can still be broken. That is, after the child class in PHP inherits the parent class, if the child class sets its own constructor, the function of the parent class will not be automatically executed. Check the code:

Class calf extends Niu // note that this is the inheritance of php.

{

Function calf ()

{

// If I have my own constructor, the parent class will be less worried.

}

}

In this case, I cannot execute "collect information" in the constructor of the parent class.

Features of constructor in PHP5

The constructor of most languages must be the same as the class name. In fact, the above method is PHP4. The founder of PHP5 replaced it with the _ construct function. of course, the original writing method is still compatible.

Let's take a look at this ultimate writing method (why is it called Ultimate? I am too tired to write this, so I have to stop and finish it)

Class father

{

Function _ construct () // Here you must either write function father () or use _ construct

{

Echo "father ";

}

}

Class child extends father

{

Function child ()

{

Echo "son"; // if child has its own constructor, it will not output "father"

// Parent ::__ construct (); // If you want to execute a father constructor, do this.

}

}

$ Child = new child ();

I will summarize the above constructor:

1. Both the father and son are born from one ancestor. The family is inherited by one vein.

2. if your son does not develop his own business (he does not have a constructor), he must tell everyone that he is the son of XX; otherwise, no one knows this son (the father's constructor will be executed automatically)

3. if my son grows up, he doesn't want to pin his head. Then he must establish his own business (define the constructor himself ). You can call out who you are. you don't need to tell others who his father is (just execute your own constructor)

4. my son discovered that he was not able to work after his career was established. sometimes he had to rely on his father to bring some contacts for his work. Therefore, he can only continue to raise his father's name (execute parent ::__ construct () to execute the constructor of the parent class)

Next let's take a look at the last code section of this chapter:

Class father

{

Function _ construct () // Here you must either write function father () or use _ construct

{

Echo "father ";

$ This-> talk ();

}

}

Class child extends father

{

Function child ()

{

Echo "son"; // if child has its own constructor, it will not output "father"

Parent: :__ construct (); // If you want to execute a father constructor, do this.

}

Function talk ()

{

Echo "please help my son ";

}

}

$ Child = new child ();

First, let's look at the parent class. you will find that an error occurs when initializing the parent class independently, because there is no talk function in the parent class. Therefore, only child can be initialized. if parent ::__ construct () is executed, the talk in the child class will be executed automatically by the constructor of the parent class.

As you can see, we will certainly have a new view and understanding of php constructor, which is actually very useful. Next I will explain the code in the above tone:

1. my father was so old as a yellow man that he could no longer help his son pick up the project, so he had to stay at home and cook the dishes (the parent class will encounter an error when initializing it separately)

2. the son ran into the resources he had brought to him by his father, and seldom went back to visit his father. He also said that his father was useless and almost never went back to visit his father.

3. one day, my son encountered a major problem in his career, which could only be solved by his father's friends.

4. However, my elderly father is ill and is lying in bed all day. He wrote a letter to his friend to give the son and father to his friend in case of suspicion.

5. the son handed the letter to his father's friend (the sub-class constructor enforces the constructor of the parent class)

6. my father's friend opened the letter, and his son's tears blurred. The letter says: please help my son (the constructor of the parent class automatically executes the subclass method)

Well, php constructor is about to talk about it. In fact, it is not only php, but any development language that is still in use has its own uniqueness. no language has a future and no future, however, the fields are different, the customer requirements are different, and the money is different.

I would like to give you another suggestion: programmers who write programs share the same principle of being human, who will be human, sensible, and caring will be very popular. Similarly, programmers who will consider for users, the written code will be liked by users, liked by leaders, and admired by colleagues.

Reference the movie "Food God" Zhou Xingchi's sentence: What is "the best dish can be made with your heart"? This heart is not in a specific dish. This heart is here, in your life, in your understanding and understanding of the world.

The above introduces happy learning php100day (7)] excellent PHP constructor: my father and son complained, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.