Classes in PHP and their inheritance relationships

Source: Internet
Author: User
Classes are inherited and cannot be opened. classes have always been a very important concept in OOP. In this era of excessive OOP, everything has to be abstracted into classes. It seems that no abstract class will write code. The so-called class is the sum of a series of objects, which have the same attributes and methods. The so-called method is the behavior that an object can operate on, such as eating, sleeping, and

Classes are inherited and cannot be opened. classes have always been a very important concept in OOP. In this era of excessive OOP, everything has to be abstracted into classes. It seems that no abstract class will write code. The so-called class is the sum of a series of objects, which have the same attributes and methods. The so-called method is the behavior that an object can operate on, such as eating, sleeping, and

Class

Class is a very important concept in OOP. In this era of over-OOP, classes have to be abstracted into classes, as if they do not have abstract classes to write code. The so-called class is the sum of a series of objects, which have the same attributes and methods. The so-called method is the behavior that objects can operate on, such as eating, sleeping, and computing.

 Weight = 20; $ a-> eat ();?>

Here is a simple animal type, which has the attribute "weight", the method "eat", and "Sleep ". These attributes and methods,Is the class visible inside or inside the class. There ispublicAndprivateKeyword.It is obvious in appearance, such as hands, feet, mouth, and food.publicThere are also some external aspects that cannot be seen, such as the heart, liver, spleen, heartbeat, and digestion. These are the so-calledprivateAndprotected. As,privateAndprotectedWhat's different, I will talk about it later. (Modified 2014-5-1)

Constructor

We can instantiate an object as follows:

 Weight = 20;?>

However, ifweightCan only be accessed internally or want to simplify the operation? The cool man designed the constructor again.__constructThis is a special name method.

 weight = $weight;    }    public function eat() { echo "it's eating!"; }    private function sleep(){ echo "it's sleeping!"; }}$a = new Animal(20);?>
Inheritance

When a class is available, finding a relationship between the class and the class indicates an obvious relationship of inheritance.Inheritance is neither simply genetic inheritance in biology nor property inheritance in economics. It only expresses a concept. You have me, and you don't have me.It is more like expansion than inheritance. Keywords are selected for many languages, including PHP.extendsI want to clearly express this concept. The derived relationship can be expressed in the family tree, just like inheritance. (Modified 2014-5-1)

 

The above expression is that humans inherit the methods of animals to "eat" and "Sleep ". However, people not only "eat and sleep", but also "think", so there is an IQ "IQ ".What should I do if some attributes and methods do not want to be accessed outside the class and want to be accessed by the class inherited from it?protectedKeyword.Well, since it is inheritance, let's take a look at Darwin's evolution and degradation theory. The derived class is based on the base class, and a new attribute or method is added, that is, progress.privateIs degraded. (Modified 2014-5-1)

Well, inheritance is not complex. The above public, protected, and so on are all written by me without logic.

Abstract class

This section is abstract. Let's tell a story! (Modified 2014-5-1)

When God is making everything, he wants to create a creature. The creature has a life to breathe, and then it is so abstract that he does not know how to create it. (Abstract base class with abstract method breathing)

Then God said, Let's make birds. Birds are creatures. They have two feet, wings, fly, breathe with lungs. What color are feathers? What does it look like? Good abstraction, no creation. (Derived abstract class)

God said, okay, let's make the swallow. The swallow is a bird, black hair, and tail hair is like scissors. Then, color the image. (Class)

Then, God uses the abstract method to create the World's largest operating system, Hello World!

 
Static methods and attributes

In the C/C ++ era, I want to explain how to interpret this static word. This is a computer expression, because it stores variables and methods in the static zone of the program, so static variables and static methods are called. I have been thinking about how to combine this in the class with reality, so that we can better understand it and use it better. OK. Think about it. There is a People class that has nothing to do with the attributes and operations of human beings? ID generated for the number of human beings.

 ID = self: create_id (); // static attributes/methods internally called} static $ populace; // static attributes, population static function () create_id {return microtime ();} // static method, generate ID card} echo "Populace :". people: $ populace; // static attributes of external calls/methods?>
To be continue

As there are still important things tomorrow, we will only talk about it today. Here we will just warm up, and the wonderful content will be later.

(End)

All Rights Reserved: the source information of the old white is reprinted. < >

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.