PHP abstract class features examples

Source: Internet
Author: User

characteristics of abstract classes

1. A class that is defined as abstract cannot be instantiated.
2. If at least one of the methods in it is declared abstract, then the class must be declared abstract.
3. A method defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.
4. When inheriting an abstract class, the subclass must define all the abstract methods in the parent class (the access control for these methods must be the same as (or looser) in the parent class. )

Demo

Abstract class abstractclass{//Mandatory subclass definition These methods abstract protected function getValue ();    Abstract protected function Prefixvalue ($prefix); Common method (non-abstract method) Public function PrintOut () {print $this->getvalue ().    "\ n"; }class ConcreteClass1 extends abstractclass{//The parent class defines two abstract methods, such as a subclass that only implements one, then reports the following error//fatal Error:class ConcreteClass1 Contains 1 abstract//method and must therefore be declared abstract or//implement the remaining methods//(AbstractClass    ::p refixvalue) protected function GetValue () {return "ConcreteClass1";  }//And the subclass must not have access to the parent class, the following error will be reported if you change the access method of the following subclass to private//fatal error:access levels to//concreteclass1::p Refixvalue () must be protected (as in//class abstractclass) or weaker public function Prefixvalue ($prefix) {return ' {$prefix}co    NcreteClass1 "; }}//when instantiating an abstract class, Fatal Error:cannot instantiate//abstract class abstractclass//$obj = new AbstractClass ();//But we can use # ######################################################## #abStract class foo{static function bar () {echo "test\n"; }}foo::bar (); ########################################################## $obj = new ConcreteClass1 (); Echo $obj GetValue ();

5, subclasses can define an optional parameter (that is, must have a default value), and the parent class abstract method of the declaration does not have, then the declaration of the two does not conflict.

Abstract class abstractclass{    //Our abstraction method only needs to define the required parameters of the abstract    protected function prefixname ($name);} Class Concreteclass extends abstractclass{    //Our subclass can define optional parameters that do not exist in the parent class signature    //If there is no default value for $separator here, the fatal error is reported: ,,//declaration of Concreteclass::p refixname () must be//compatible with that of AbstractClass::p refixname () public    function Prefixname ($name, $separator = ".") {        if ($name = = "Pacman") {            $prefix = "Mr";        } elseif ($name = = "Pacwoman") {            $prefix = "Mrs";        } else { c10/> $prefix = "";        }        Return "{$prefix} {$separator} {$name}";}    } $class = new Concreteclass;echo $class->prefixname ("Pacman"), "\ n", Echo $class->prefixname ("Pacwoman"), "\ n";

6, abstract classes can have no abstract method, in turn, the abstract method of the class must be declared as abstract class

Abstract class A{public     function Show () {         echo ' A ',     }} class B extends a{public     function Hello () {         EC Ho ' B ';                  Parent::show ();     } }  $obj = new B;  $obj->hello (); BA

7. The order of abstract class definitions

Correct definition Order abstract class Horse extends animal {public     function get_breed () {return "Jersey";}}  Class Cart extends Horse {public     function get_breed () {return "Wood"}}//Wrong definition order will cause horse class to not find class cart Exten DS Horse {public     function get_breed () {return "Wood";}}  Abstract class Horse extends animal {public     function get_breed () {return "Jersey";}}

8, the abstract method can be static method

Abstract class foo{     abstract static function dump ();}  Class BAR extends foo{     static function dump () {var_dump ("BAR");}} BAR::d UMP (); String (3) "BAR"

9. When an abstract class inherits another abstract class, the abstract method does not need to be declared abstract.

Fatal Error:can ' t inherit abstract function//class1::somefunc () (previously declared abstract in//class2) abstract CLA SS Class1 {    Abstract public Function somefunc ();  }  Abstract class Class2 extends Class1 {    abstract public Function somefunc ();  }//Below is the correct abstract class Class1 {
  
   abstract Public Function SomeFunc ();  }  Abstract class Class2 extends Class1 {  }
  

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.