PHP Inheritance class extends usage

Source: Internet
Author: User
Tags gety
class inheritance is important, and as a programmer it is essential to deal with him every day, but do you really know something? The following is seen from the official web site.

A class can inherit the methods and properties of another class in a declaration, using extends as a keyword. PHP does not support multiple inheritance, and a class can inherit only one base class.
Inherited methods and properties can be overwritten by re-declaring them with the same name. However, if the parent class defines a method with final, the method cannot be overwritten. The overridden method or property can be accessed through the parent::.
When overriding a method, the parameters must be consistent or PHP will issue an E_STRICT level error message. However, with the exception of constructors, constructors can use different parameters when overridden.
We all know about the basic concept, but for final and parent: I really haven't used it in the project, I'm ashamed of it.
Here are the main things to say about the use of these two keywords.

Final keyword

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

Scope resolution operator (::)

The scope resolution operator, or more simply a pair of colons, can be used to access static members, class constants, and to override properties and methods in a class.
Self,parent and static These three special keywords are used to access their properties or methods within the class definition.

When a subclass overrides a method in its parent class, PHP does not invoke methods that have been overridden in the parent class. Whether the method of calling the parent class depends on the child class. This mechanism is also used for constructors and destructors, overloading, and magic methods.

The following is an example of a method that invokes a parent class:

<?phpclass myclass{    protected function MyFunc () {        echo "myclass::myfunc () \ n";    }} Class Otherclass extends myclass{//    overrides the definition of the parent class public    function MyFunc ()    {        //But can still call the overridden method        in the parent class Parent::myfunc ();        echo "Otherclass::myfunc () \ n";    }} $class = new Otherclass (); $class->myfunc ();/** * Output result * MYCLASS::MYFUNC () * OTHERCLASS::MYFUNC () */?>

Use the class name when referring to these items outside of the class definition.
:: Class since PHP 5.5, keyword class can also be used to parse the class name. With Classname::class you can get a string that contains the fully qualified name of the class ClassName. This is especially useful for classes that use namespaces. These are probably the topics to be discussed later on, and that's what we'll discuss later, and we're still using php5.3.
But to be honest, there are some keywords that are really hard to use in small projects, but I want to know more about what is needed. Especially when you're using a third-party class library, you can always learn something when you study his program logic. Although some things you do not use for a long time, may forget, but after all, he has been in your mind, will always for the future of life to leave some traces.
The last nonsense to say, to share a range of analytic operators (::) application code, you also feel the following:

<?phpclass ca{/** * Default value for directly used test properties */protected static $item = ' Foo ';    /** * Default value of the test attribute used indirectly */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{/** * Redefine default values for test properties */protected static $item = ' Bar ';    public static function Setother ($val) {self:: $other = $val; }/** * Do not re-declare method () methods */}class CC extends ca{/** * Redefine default values for test properties */protected static $item = ' T    Ango '; public static Function method () {print self:: $item. "         \ r \ n "; Print self:: $other. "     \ r \ n "; }/** * Do not re-declare Setother () method */}class CD extends ca{/** * Redefine default values for test properties */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 Results * foo * CB * Tango * CC * Foo * CD */?>

PHP extends class inheritance code example:

< PHP   class a{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 a{}   /*extends is an inheritance function */   $b 2=new A2 (10,10);   echo $b 2->getx (). " <br> ";   echo $b 2->gety ();   ? >

The above-described content is the PHP extends class inherits all the implementation steps.

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.