PHP Abstract class inherits abstract class abstract class

Source: Internet
Author: User
Occurrence Reason: parent classOf uncertainties

Why design abstract class this technology?

1. in real-world development, we may have a class that is the parent of other classes , but it does not need to be instantiated by itself, and the main purpose is to let subclasses inherit. This achieves code reuse, while benefiting project designers and design classes.

2. keywords :abstract

3. Basic usage :

Abstract class name

{// method

// Properties

abstract modifier function function name ( Parameter list );

/ *theabstract modifier function function name ( parameter list )

{

Echo " This is a wrong formulation! ";// because the method body is written more

}

*/

}

Abstract modifies a class, which is called an abstract class;

Abstract Modify a method that is called an abstract method "if it is an abstract method, there cannot be a method body";

4. precautions :

a . Abstract classes can have no abstract methods, but also can have implemented methods (complete functions);

B. once a method declares Abstract method, you must declare that the class is Abstract class, which is Abstract class name corresponding Abstract method;

c. if a class inherits an abstract class, it must Inheritance of the abstract class. All abstract method, unless it is an abstract class;

Case 1 :

abstract class Animal// parent class

{

Public $name;

protected $price;

abstractPublic Function Cry ();

// there is no method body, this method is mainly to let subclass to implement

}

class Dog extends Animal// sub-class

{

Public function Cry ()// subclass Inherits Parent class method

{

echo " The Puppy barks! ";

}

}

$dog 1=new Dog ();

$dog 1->cry ();//

?>

The above introduces the PHP abstract class, including PHP, abstract class content, I hope that the PHP tutorial interested in a friend helpful.

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