[Happy Learning PHP100 Day (vii)] explaining PHP constructor: Father and son's kindness and resentment _php tutorial

Source: Internet
Author: User
Tags learn php php compiler
Foreplay section:


One or one words summarize PHP:

PHP is used to do Web applications, which is very suitable for the site, the famous WordPress is the development of PHP. For applications other than web, do not consider PHP.

Some of the key words that are explained in this article include:

High on the big: high-end atmospheric grade abbreviation. Internet is too popular, netizens are too lazy, said all unhappy


Several versions of PHP:

PHP4 should be said to be a milestone version of PHP, such as the introduction of Zend engines, various high-speed acceleration. But this version is too weak for the object, ignoring PHP4 is right.

PHP5 if you want to develop please download version 5.3, the most widely used, relatively stable. 5.0~5.2 please delete, please use caution after 5.4, one because it has not been popularized, one because it is compatible with the previous version 5.4 (online many open source class libraries are incompatible with 5.4 later version)

PHP6 you can download to me admire you.


Learn about PHP:

If you just want to quickly learn PHP syntax, please use the PHP compiler installed under Windows, one-click installation configuration is simple, do not recommend the direct use of Linux to learn PHP, when you configure the correct, you are not interested in PHP.

If you learn to fit into a business environment, be sure to learn the configuration and development of PHP under Linux. After all, a lot of good third-party support is native to Linux, and efficiency and stability is the best Linux. Of course, PHP in IIS7 support is said to be good, but please don't take it too seriously, boring.


Second, the body part:

What is a constructor function

Each language (such as Java, C #, PHP) constructors are somewhat different in character. A function that can be executed automatically when each object is initialized, and the function does not set any content, and if you set it, it will automatically execute. More important, you cannot manually invoke this function externally.

Generally speaking, most of the language construction function name and class name is the same, here PHP is outside the column, it is the same, but to the php5 although it can still be the same, but it is not recommended to use the same name as the same as the name of the function.

Why do we have constructors

If it is a separate single class. In fact, the constructor function is also "convenient" role.

For Example:

Class A

{

function A ()

{

Here to do some initialization work

}

}

Does that make sense? It doesn't really make sense, you can simply put the thing inside a () on the outside to invoke.

When and where does the constructor make sense? In fact, in the object and object inheritance, give a story of the example :

For example : After a year, I finally a mad dog development to complete a very high-class library of functions, and think that this set of libraries have no similar domestic goods, in order to prevent others to understand, I also write the structure of the class library is extremely complex. Then accidentally sold to a number of programmers. But what? I would like to do some statistics, that is, I want to know who has used my library, so that I feel good about myself, so I designed my base class library:

Class Bull//This is a high-class library name

{

function Bull ()

{

Here furtive to the current user of some local information to collect, secretly sent to my mailbox, very no moral integrity have wood?

}

Here are the other high-up functions and methods that can be called externally, slightly

}

Here we assume: If programmers want to use my features, they have to inherit my library, otherwise you simply don't understand how to invoke them:

Class Calf extends cow//Note this is just the way PHP inherits.

{

Here, do whatever you want. Anyway, you're going to get a message from me before you do it, because the parent class I've set the constructor.

}

So you know it:

1, the constructor is not for the convenience of the resulting

2, more for the object-oriented structure design and use

3, its inheritance, controllability, System is the main purpose of the structure function, product-oriented, commercialization is the main purpose of its use

4, if you will develop iOS, you will see that you write the view if you inherit UITableView, then the final interface will automatically be rendered table. Do you want to know how Apple realizes UITableView? No door, honest inheritance, honestly on my basis to write code is kingly.

5, program writing is not said to write elegant, beautiful, art, ugly understand is a good program, any program written in writing when the owner has his own purpose. In the same way, the boss of a wrist has a purpose for every word you say, not to show how good his eloquence is.

Features of PHP's constructors

The above restrictions seem to be fine, but they can still be broken. That is, after the subclass of PHP inherits the parent class, if the child class sets its own constructor , then the function of the parent class will not be executed automatically. Look at the code:

Class Calf extends cow//Note this is just the way PHP inherits.

{

function Calf ()

{

I have my own constructor, and the parent is less worried.

}

}

So I can't do the dirty thing about collecting information in the parent class's constructor.

The characteristics of constructors in PHP5

Most language constructors must be the same as the class name, in fact the above notation is PHP4. To the founder of the PHP5 with __construct this function to replace, of course, the original wording is still compatible

Let's look at the ultimate way of writing it (why is it called the ultimate? Because I was too tired to write this, I had to stop and finish it.

Class father

{

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

{

echo "Father";

}

}

Class Child extends Father

{

function Child ()

{

echo "Son"; If the child has its own constructor, it will not output "father".

Parent::__construct (); If the constructor of the father is to be executed, it must be done, and there is no other way.

}

}

$child =new Child ();

The above constructors I summarize:

1, father and son are an ancestor born, family, a vein of inheritance.

2, if the son is not married (own no constructor), then the son must tell everyone he is the son of XX, otherwise no one knows this son ( father's constructor will force automatic execution )

3, if the son grew up, do not want to let father tube head of the foot. Then he had to get his own ( define the constructor ). You can call out who you are, and you don't have to tell anyone who his father is. ( executes only his own constructor )

4, son Liye found no, and sometimes rely on his father with some network to give him contact project do. So he can only continue to mention his father's name outside ( execute parent::__construct () to execute the parent class constructor )

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 the child has its own constructor, it will not output "father".

Parent::__construct (); If the constructor of the father is to be executed, it must be done, and there is no other way.

}

function talk ()

{

echo "Please help my son";

}

}

$child =new Child ();

Looking at the parent class first, you will find that it is an error to initialize the parent class individually because there is no talk function in the parent class. The subclass (child) can only be initialized if Parent::__construct () is executed; You will find that the talk in the subclass is executed, which is actually performed automatically by the parent class's constructor.

You see this, will certainly have the PHP constructor function to have the new view and the understanding, actually this really is very useful. I'm going to follow the above rhythm and explain this code in the tone above:

1, the father finally one day autumnal matron, can not go out to help his son to pick up the project, so can only stay at home burning vegetables ( parent class alone initialization will be wrong )

2, the son relied on the former father to bring him the resources rampage, and seldom go back to visit his father, foreign also said his father really useless, almost want to never go back to visit father

3, finally one day, the son in the cause of big problems, and this problem only his father's friends can solve.

4, however, the old father was ill, lying in bed all day, for his son's father did not forget to write a letter to his friend, ordered the son must give his friend to face to dismantle

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 of Tears blurred, the letter reads: Please help my son (the parent class's constructor automatically executes the subclass method )

Well, PHP constructor is about the same, in fact, not only PHP, any development language is still in use now has its unique, no language has a future which has no future, but the field is different, customer requirements, different money.


Here again to give everyone a suggestion: write procedures and the same truth, will be a man, Ming affairs, caring people must be very popular; Similarly, programmers who will be considered by the user, the code will be written out by users like, leaders like, colleagues worship.

Quote movie "Food God" Stephen Chow's sentence: What is called "intentions to make the best food", this heart is not in a specific dish, this heart is here, in life, in your understanding of the world and sentiment.


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

Follow my public number, grow up a little bit every day
Follow the steps:
Very straightforward to take out the mobile phone, open, and click Sweep, sweep the following QR code can be

Enter Php1, PhP2, PhP3, php4 to view lectures in

http://www.bkjia.com/PHPjc/735869.html www.bkjia.com true http://www.bkjia.com/PHPjc/735869.html techarticle Foreplay part: One or one sentence summary php:php is used to do Web applications, which is very suitable for the site, the famous WordPress is PHP development. Applications other than the Web must not be ...

  • Related Article

    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.