PHP class Inheritance extends use introduction

Source: Internet
Author: User
Tags class definition gety inheritance php language php class

  We have introduced in this article the implementation of the PHP class inheritance extends, hopefully for beginners to improve their ability to program in the PHP language

It's been a long time coming out of work, but there's a lot of experience in the project, but when it comes to the bottom of things, it's often silent. Now the bottom of the project design is less and fewer, can be said to be really used is that a little bit, the real core of things have been encapsulated by the framework. Always feel that they have been progressing very slowly, on the underlying design ideas, often also sensed. With the passage of time, we often feel that when the book to hate less, the next must be a bad complement design ideas.   Let's review class inheritance today.   Class inheritance is very important, as a programmer also basic daily deal with him, but there are some things you really know? The following is seen from the official website.   A class can use extends in a declaration, and the keyword inherits the methods and properties of another class. PHP does not support multiple inheritance, a class can inherit only one base class.   Inherited methods and properties can be overridden by a declaration with the same name. However, if the parent class defines a method that uses final, then the method cannot be overwritten. You can access the overridden method or property through Parent::.   When overriding a method, the parameters must be consistent or PHP will issue e_strict-level error messages. However, with the exception of constructors, constructors can use different parameters when overridden.   about the basic concept everyone will know, but for final and parent:: In the project I did not really how to use, ashamed ah. The following is mainly about the use of these two keywords.   Final keyword   PHP 5 new keyword, the subclass cannot overwrite the method if the method in the parent class is declared Final. Similarly, if a class is declared final, it cannot be inherited. It should be noted that attributes cannot be defined as final, and only classes and methods can be defined as final.   Scope parsing operators (::)   range resolution operators or, more simply, a pair of colons, can be used to access static members, class constants, and can be used to override properties and methods in a class. Self,parent and static These three special keywords are used to access the properties or methods within the class definition.   When a subclass overwrites a method in its parent class, PHP does not invoke methods that have been overridden in the parent class. The method of calling the parent class depends on the subclass. This mechanism is also used for constructors and destructors, overloads, and Magic methods.   Below is an example of a method that invokes a parent class: The code is as follows: <?php class MyClass {    protected function MyfUNC () {        echo "Myclass::myfunc () n";    }}   class Otherclass extends MyClass {    /covers the definition of the parent class     Public Function myFunc ()     {       //But still can call the parent class Methods covered in         Parent::myfunc ();         echo "Otherclass::myfunc () n";    }}   $class = new Otherclass (); $class->myfunc (); /**  * output->  * myclass::myfunc ()  * otherclass::myfunc ()  */?>       When in class definition When you use these items, you use the class name.  :: Class from PHP 5.5, keyword class can also be used for the resolution of the name. Using 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 may be the topics to be discussed in the future, the concept that is to be discussed later, now we are still using php5.3.   But to be honest, there are some keywords that are really hard to use in small projects, but I'd like to know more about what's always needed. Especially when you're using a third party class library, when you look at his program logic, you can always learn something. Although some things you do not use for a long time, may forget, but after all he has passed in your mind, always for the future of life leave traces.   Last nonsense to say, share a range of analytic operators (::) The application of the code, you also come to feel the following:     code as follows: <?php   class CA {   /**   &NBSP ;  * default value for test properties directly used      */    protected static $item = ' Foo ';      /**      * The default value of the test properties used indirectly      */    protected static $other = ' CA ';       public static function method ()     {        print self:: $item. " RN ";         print self:: $other. " RN ";    }       public static function Setother ($val)     {        SEL F:: $other = $val;    }}   class CB extends CA {   /**      * redefining the default value of 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 for test propertiesValue      */    protected static $item = ' Tango ';       public static function method ()     {        print self:: $item. " RN ";          print self:: $other." RN ";     }      /**      * do not re-declare Setother () method      */} &nbs P Class CD extends CA {   /**      * redefining Test Properties Default value      */    protected S Tatic $item = ' Foxtrot ';      /**      * do not re-declare any method to achieve 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->  * foo  * cB  * Tango  * cC  * Foo  * cD  */ ?>       PHP extends class inheritance code example:   Codeas follows: < PHP    class a{   public $x    public $y    function __construct ($x =0 , $y =0) {   $this->x= $x    $this->y= $y;   }    function Getx () { & nbsp return $this->x;   }    function Gety () {   return $this->y;   }    function __destr UCT () {}   }    class A2 extends a{}   /*extends is an inherited function */   $b 2=new A2 (10,10);    echo $b 2->getx (). <br> ";    echo $b 2->gety ();   ?>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.