The use of _php in the inheritance of classes in object-oriented PHP tutorial

Source: Internet
Author: User
Tags php language
The inheritance of PHP classes is an important point of knowledge in the learning of PHP language. So how do we properly learn the 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 we have children, is it to prevent the old!) You have some of your genes and your wife's genes, and you have to regenerate them into a new individual, and this new personality will certainly contain the characteristics of the two of you, which is a biological interpretation of heredity (inheritance). In the world of programming is this heredity is inheritance!

First of all, after understanding some of the living principles of inheritance, I would like to look at the inheritance of the PHP class is not so mysterious. Perhaps it is not mysterious, because we are too complex. To inherit, you have to have a "root cause", the "root" you may imagine you have a son or daughter, they will get some "things (properties and methods)" from you, so that your "offspring" is holding all of your (root) characteristics. Here's a syntax to describe how this is expressed in PHP object-oriented (not as straightforward as human beings, married, and over time your offspring are produced)

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

Syntax: Class father{

}

1. Produce "Descendants" (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, so that your child class has only the parent class all the properties and methods. It's really that simple.

Let's do some practical things now, after all, the inheritance of the PHP class defines a parent class and subclass to complete a task! Here this task is more monotonous, on the person, people have a name (attribute), people have to sleep and eat (method). We will use this basic task to complete the knowledge of this section.
Class father{
protected $name;
function __construct ($name) {
$this->name= $name;
}
function __destruct () {
echo "

{$this->name} is dead, too.

";
}
This is called a constructor, which is used to initialize
function go_to_sleeping () {
echo "

{$this->name} want to sleep.

";
}
Function Eat () {
echo "

{$this->name} want to eat.

";
}
}
Class Son extends father{
function playing () {
The child will be very naughty, of course, he also wants to eat to sleep the creature
echo "

{$this->name} is making mischief ...

";
}
}
$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 ();
?>
Class father{
protected $name;
function __construct ($name) {
$this->name= $name;
}
function __destruct () {
echo "

{$this->name} is dead, too.

";
}
This is called a constructor, which is used to initialize
function go_to_sleeping () {
echo "

{$this->name} want to sleep.

";
}
Function Eat () {
echo "

{$this->name} want to eat.

";
}
}
Class Son extends father{
function playing () {
The child will be very naughty, of course, he also wants to eat to sleep the creature
echo "

{$this->name} is making mischief ...

";
}
}
$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 constructors mentioned in PHP's constructor and the keywords in the package of PHP classes, and if there's anything you don't understand, go and see! I do not want to say more, noon did not want to sleep ah. Tell me about this little program.

In the father of the class, we define the general characteristics, such as the name of the person, 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 use destructors to "destroy" objects, but you may not find that there are no constructors or destructors within subclasses, so subclasses are all ways to inherit from the parent, or else how can you $my_son->go_to_ Sleeping (); so called, this is the inheritance of the PHP class.

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

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 keep". $num. " Article ". $username;
}
function Jiegou () {
When using a function in a class, use this to refer directly to the
$this->yingxiaoji ();
echo "All sold";
}
}
The extends of the PHP class 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 "
";

Re-assigning values to variables in a class
$myname->username= "Duckling";
$myname->num= "20";
$myname->yingxiaoji ();
echo "
";

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

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

echo "
";
Because it is an inheriting class, the properties of its base class are all there, and it is possible to pass values directly to the variables of the base class.
$myname _extend->username= "Duckling";
$myname _extend->num=100;
$myname _extend->color= "BLACK";
$myname _extend->jiegou ();
$myname _extend->mycolor ();
?>

http://www.bkjia.com/PHPjc/486073.html www.bkjia.com true http://www.bkjia.com/PHPjc/486073.html techarticle The Inheritance of PHP classes is an important point of knowledge in the learning of PHP language. So how do we properly learn the inheritance of PHP classes? In PHP object-oriented programming, the inheritance of classes is always ...

  • 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.