PHP Object-oriented abstract class and interface understanding

Source: Internet
Author: User

Combined with online documents and official documents, self-test summary experience, practical knowledge, time in a hurry, write the unknown please understand!

Combined with example analysis:

<?php

Header ("Content-type:text/html;charset=utf-8");

Abstract class people{

protected $age = 22;

Public $height = 1.70;

Abstract function work ();

Abstract methods have no method body, and subclasses must implement them.

function run () {}

function people ()//can define constructors

{

echo "average human height". $this->height. " <br/> ";

}

}

Abstract class student extends people{

If the subclass does not all implement all the abstract methods in the abstract class, then the subclass is also an abstract class

Abstract function Student_run () {}

}

Class Phpteacher extends student implements common{

Public $height = 1.75;

The property access control for the//subclass must be the same or even broader as the parent class

/*function Phpteacher ()

{

echo "PHP Programmer's average height". $this->height. " <br/> ";

}*/

function work () {//abstract method subclasses must implement

echo "Professor Php";

}

function Getage ()

{

Echo $this->age;//inherits the parent class can access the age of the parent class

}

function Run () {

echo "will run";

// }

function Speakenglish () {

echo "speaks English." <br/> ";

}

function Drive ($height)

{

echo $height. " <br/> ";

}

}


Interface common{

//All interface methods, the class that implements the interface must be implemented

function Speakenglish ();

Interface are completely abstract, can only declare methods, and can only declare methods of public, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables to be abstract

function drive ($args);

}

$phpTeacher = new Phpteacher ();

$phpTeacher->work ();

echo "<br/>";

$phpTeacher->getage ();

/*

Summarize:

Abstract class

PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private. The method must be called in the same way, i.e. the type and the required number of parameters must be the same. For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different.

Features of abstract classes:

    • Abstract classes cannot be instantiated and can only be inherited.

    • Abstract classes do not necessarily have abstract methods, classes with abstract methods, must be abstract classes.

    • The visibility of an abstract method cannot be private

    • Abstract methods in subclasses, need to be overridden.

When do I need to use abstract classes?

    • There is a method, the method body does not know how to write, subclasses must also have this method, encapsulated into an abstract method, the class is an abstract class.

    • You can use abstract methods when you must encapsulate certain methods in a control subclass.

    • When the required control class can only be inherited and cannot be instantiated.

Object interfaceusing Interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods. interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty. All methods defined in the interface must be public, which is the attribute of the interface. interface Inheritance (extends):
    • Interface Inheritance Interface Interface Interface name extends parent interface name

Note: The inheritance of a class is a single inheritance (only one parent), but the inheritance of the interface is multiple inheritance, and the implementation of the class to the interface is also multi-implemented.

Interface implementation (implements):

    • Class implements interface class name implements Interface Name 1, interface Name 2, ...

Inheriting classes implement interfaces at the same time:

The class inherits the parent class and implements the interface class name extends the parent class name implements interface name


The difference between an abstract class and an interface
    • An interface is a special abstract class that contains only abstract methods and no member properties.

    • class Implementation ( implements) interface, you must fully implement all the methods in the interface, and class inheritance (extends) abstract classes only need to override the abstract methods that are needed.

    • Abstract classes can only be inherited, but interfaces are multi-inheritance, and class-to-interface implementations are also multiple implementations.

*/

?>

Reference Link: http://php.net/manual/zh/language.oop5.abstract.phphttp://php.net/manual/zh/ language.oop5.interfaces.phphttp://www.jb51.net/article/47485.htmhttp://blog.csdn.net/lamp_yang_3533/article/ details/50830214

PHP Object-oriented abstract class and interface understanding

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.