The use of inheritance of classes in object-oriented PHP

Source: Internet
Author: User
Tags define constructor inheritance php language variables php class sleep

The inheritance of PHP class is a more important knowledge point in the learning of PHP language. So how do we learn the correct inheritance of PHP classes? In PHP object-oriented programming, class inheritance is always the most critical.

This is like a human having children (why must have children, is to prevent the old!) You put some of your genes and your wife's genes out into a new individual, and this new personality will certainly encompass the characteristics of the two of you, which is a biological explanation of heredity (inheritance). In programming the world is this heredity is inheritance!

First of all, after learning some of the living principles of inheritance, I want to see if the inheritance of the PHP class is no longer so mysterious. Perhaps it is not mysterious, because we are too complicated. To have inheritance is to have a "source", the "root" you may imagine that you have a son or a daughter, they will get some "things (properties and methods)" from you, so that your "offspring" is holding your (root) all the characteristics of the. Here's a syntax to describe how this is expressed in a PHP object (it's impossible to be as direct as humans, to get married, and your offspring will be there after a while).

1. Generate "root cause" class (parent class or base class)

Syntax: Class father{

}

1. Produce "descendant" (subclass)

Syntax: class son extends father{

}

Description: The parent class is just a normal class, if you want to have descendants you just need to add a extends keyword after the normal class to OK, so that your subclass only has all the attributes and methods of the parent class. It's just that simple.

Let's do some practical things, after all, the PHP class in the inheritance of the definition of a parent class and subclass is to complete a task! Here this task is more monotonous, in terms of human beings, people have names (attributes), people want to sleep and eat (method). We will use this basic task to complete the knowledge of this section.
<?php
Class father{
protected $name;
function __construct ($name) {
$this->name= $name;
}
function __destruct () {
echo "<p>{$this->name} is also a dying <br/></p>";
}
This is called the constructor, which is used to initialize the
function go_to_sleeping () {
echo "<p>{$this->name} Want to sleep .</p>";
}
Function Eat () {
echo "<p>{$this->name} want to eat .</p>";
}
}
Class Son extends father{
function playing () {
Children will be very naughty, of course, he is also to eat to sleep the creature
echo "<p>{$this->name} is being mischievous ...</p>";
}
}
$your _father=new father ("Dad");
$your _father->go_to_sleeping ();
$your _father->eat ();
$my _son=new son (' baby ');
$my _son->go_to_sleeping ();
$my _son->eat ();
$my _son->playing ();
?>
<?php
Class father{
protected $name;
function __construct ($name) {
$this->name= $name;
}
function __destruct () {
echo "<p>{$this->name} is also a dying <br/></p>";
}
This is called the constructor, which is used to initialize the
function go_to_sleeping () {
echo "<p>{$this->name} Want to sleep .</p>";
}
Function Eat () {
echo "<p>{$this->name} want to eat .</p>";
}
}
Class Son extends father{
function playing () {
Children will be very naughty, of course, he is also to eat to sleep the creature
echo "<p>{$this->name} is being mischievous ...</p>";
}
}
$your _father=new father ("Dad");
$your _father->go_to_sleeping ();
$your _father->eat ();
$my _son=new son (' baby ');
$my _son->go_to_sleeping ();
$my _son->eat ();
$my _son->playing ();
?>
Parsing: In our first example of using inheritance, we used the constructor mentioned in the PHP constructor and the keyword in the encapsulation of the PHP class, and if there's something you don't understand, go and see it! I don't want to say any more, I don't want to sleep at noon. Talk about this little program.

In the father of the class, we define the general characteristics, such as the person's name, the person to eat and sleep, and then in its subclasses (descendants) we define a personalized method (playing), after all, there is a different place between people. We use constructors to initialize this name, and of course we use destructors to "kill" off objects, but you may not find that there are no constructors and destructors in subclasses, so subclasses are all methods that inherit from the parent, otherwise how can you $my_son->go_to_ Sleeping (); so called, this is the inheritance of the PHP class.

Article quotes from: http://club.topsage.com/thread-501298-1-1.html

<?php
Class my_class{
var $username = "Chick";
var $num = 10;
function Yingxiaoji () {
Using global variables in a class
$num = $this->num;
$username = $this->username;
echo "I got it". $num. " Article ". $username;
}
function Jiegou () {
When using a function in a class, use this to refer directly to the
$this->yingxiaoji ();
echo "Sold it All";
}
}
The PHP class's extends represents inheritance, but PHP does not allow multiple base classes to be inherited at the same time
Class My_class_extend extends my_class{

var $color = "Red";
function MyColor () {
$color = $this->color;
$username = $this->username;
echo ", this only". $username. " Is ". $color;
}
}
$myname =new My_class (); Instantiation of a class
$myname->yingxiaoji ();
echo "<br>";

To assign a value to a variable in a class
$myname->username= "Little Ducks";
$myname->num= "20";
$myname->yingxiaoji ();
echo "<br>";

$myname->jiegou ();
echo "<br>";

Initializes an inherited class of a class
$myname _extend=new my_class_extend ();
$myname _extend->jiegou ();
$myname _extend->mycolor ();

echo "<br>";
Because it is an inherited class, the properties of its base class are all available, and you can pass values directly to the base class's variables
$myname _extend->username= "Little Ducks";
$myname _extend->num=100;
$myname _extend->color= "BLACK";
$myname _extend->jiegou ();
$myname _extend->mycolor ();
?>




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.