Introduction to using PHP class to inherit extends

Source: Internet
Author: User
In this article, we will introduce in detail the implementation methods for inheriting extends from PHP classes. I hope that new users will be able to improve their abilities in PHP programming for so long, there are a lot of project experiences, but when asked about the underlying things, they often say nothing. At present, there are fewer and fewer underlying things in project design, which can be said to be actually used. the core things have been encapsulated by the framework. I always feel that my progress has been slow. I often only agree with the underlying design ideas. With the passage of time, we often feel less hate when books are used. Next, we must make up for the design idea.

Let's take a look at class inheritance.

Class inheritance is very important. as a programmer, I usually deal with him every day. but do you know anything? The following is what we can see on the official website.

A class can use extends in the declaration. The keywords inherit the methods and attributes of another class. PHP does not support multiple inheritance. a class can only inherit one base class.

The inherited methods and attributes can be overwritten by declaring them with the same name. However, if final is used when the parent class defines a method, the method cannot be overwritten. You can use parent: To access overwriting methods or attributes.

If the override method is used, the parameters must be consistent. otherwise, PHP will issue an E_STRICT error message. Except constructors, constructors can use different parameters when overwritten.

Everyone will know about the basic concepts, but for final and parent: I have never used them in projects.
The following describes the usage of these two keywords.

Final keywords

A new keyword added in PHP 5. if the method in the parent class is declared as final, the subclass cannot overwrite the method. Similarly, if a class is declared as final, it cannot be inherited.
Note that attributes cannot be defined as final. only classes and methods can be defined as final.

Range resolution operator (::)

The range resolution operator or, more simply, a colon can be used to access static members, class constants, and override attributes and methods in the class.
The three special keywords self, parent, and static are used to access their attributes or methods within the class definition.

When a subclass overwrites the methods in its parent class, PHP does not call the methods already overwritten in the parent class. Whether to call the method of the parent class depends on the subclass. This mechanism also applies to constructor and Destructor, overload, and magic methods.

The following is an example of calling the parent class method:

The code is as follows:


Class MyClass
{
Protected function myFunc (){
Echo "MyClass: myFunc () \ n ";
}
}

Class OtherClass extends MyClass
{
// Overwrite the definition of the parent class
Public function myFunc ()
{
// But you can still call the method that is overwritten in the parent class.
Parent: myFunc ();
Echo "OtherClass: myFunc () \ n ";
}
}

$ Class = new OtherClass ();
$ Class-> myFunc ();
/**
* Output result->
* MyClass: myFunc ()
* OtherClass: myFunc ()
*/
?>

The class name is used when these items are referenced outside the class definition.

: Class starts from PHP 5.5, and the keyword class can also be used for parsing class names. You can use ClassName: class to obtain a string that contains the fully qualified name of the class ClassName. This is especially useful for classes that use namespaces. These may be the topics to be discussed later. we will discuss this concept later, and we are still using php5.3.

But to be honest, some keywords are really difficult to use in small projects, but I want to know more about them. Especially when you use third-party class libraries, you can always learn something when you carefully study the program logic. Although you may forget something you don't need for a long time, after all, after all, he has been in your mind and will always leave some marks for your future life.

Finally, I will not talk much about it. I will share the code of the range parsing operator (: :). you will also feel it:

The code is as follows:


Class cA
{
/**
* Default value of the directly used test property
*/
Protected static $ item = 'foo ';

/**
* Default values of indirect test properties
*/
Protected static $ other = 'CA ';

Public static function method ()
{
Print self: $ item. "\ r \ n ";
Print self: $ other. "\ r \ n ";
}

Public static function setOther ($ val)
{
Self: $ other = $ val;
}
}

Class cB extends cA
{
/**
* Redefinition of the default value of the test property
*/
Protected static $ item = 'bar ';

Public static function setOther ($ val)
{
Self: $ other = $ val;
}
/**
* Do not re-declare the method () method
*/
}

Class cC extends cA
{
/**
* Redefinition of the default value of the test property
*/
Protected static $ item = 'tnang ';

Public static function method ()
{
Print self: $ item. "\ r \ n ";
Print self: $ other. "\ r \ n ";
}

/**
* Do not re-declare the setOther () method
*/
}

Class cD extends cA
{
/**
* Redefinition of the default value of the test property
*/
Protected static $ item = 'Foxtrot ';

/**
* Do not re-declare any method to implement the above process
*/
}

CB: setOther ('cb '); // cB: method ()!
CB: method (); // cA: method ()!
CC: setOther ('CC'); // cA: method ()!
CC: method (); // cC: method ()!
CD: setOther ('CD'); // cA: method ()!
CD: method (); // cA: method ()!

/**
* Output result->
* Foo
* CB
* Tango
* CC
* Foo
* CD
*/

?>

PHP extends class inheritance code example:

The code is as follows:


<? Php
Class {
Public $ x;
Public $ y;
Function _ construct ($ x = 0, $ y = 0 ){
$ This-> x = $ x;
$ This-> y = $ y;
}
Function getx (){
Return $ this-> x;
}
Function gety (){
Return $ this-> y;
}
Function _ destruct (){}
}
Class a2 extends {}
/* Extends is an inherited function */
$ B2 = new a2 (10, 10 );
Echo $ b2-> getx ()."
";
Echo $ b2-> gety ();
?>


The above describes all the implementation steps of the PHP extends class inheritance.

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.