Introduction to PHP Object-oriented inheritance, polymorphism, encapsulation

Source: Internet
Author: User
Tags php programming php programming language
1. Interface
In the PHP programming language, an interface is an abstract type and a collection of abstract methods. Interfaces are usually declared in interface. The method by which a class implements an interface by implementing an interface (abstract method).

Interface Definition:

Interface interanimal{public        function speak ();        Public Function name ($name);    } The interface implements class cat implements interanimal{public        function speak () {            echo "speak";        }        Public Function name ($name) {            echo ' My name is '. $name;        }    }

Special attention:
* Classes are all abstract (no need to declare abstract)
* Interface Abstract method is public
* Members (fields) must be constants

2. Inheritance
A class that inherits from another class is called a subclass of that class. This relationship is usually likened to the parent class and the child. Sub-class will continue to
The attributes of the parent class. These attributes are made up of properties and methods. Subclasses can add new functionality outside of the parent class, so subclasses Also
is called the "extension" of the parent class.
In PHP, class inheritance is implemented by the extends keyword. Classes that inherit from other classes become subclasses or derived classes, child
Class is a parent class or a base class that inherits classes.

Class Computer {    private $_name = ' Lenovo ';    Public Function __get ($_key) {        return $this->$_key;    }    Public Function Run () {        echo ' parent ' Run method ';    }} Class Notebookcomputer extends Computer {} $notebookcomputer = new Notebookcomputer (); $notebookcomputer->run ();  Inherit the Run () method from the parent class echo $notebookcomputer->_name;  Get a private field by magic function __get ()

Special attention:
Sometimes you do not need the fields and methods of the parent class, and you can modify the fields and methods of the parent class by overriding the subclasses.

Class Computer {public    $_name = ' Lenovo ';    protected function Run () {        echo ' I am the parent ';    }} Rewrite its fields, methods class Notebookcomputer extends computer {public    $_name = ' IBM ';    Public Function Run () {        echo ' I am a subclass ';    }}

Calling a method of a parent class by overriding
Sometimes we need to be able to invoke the method content of the parent class through the overridden method, which must be used
Syntax: The parent class Name:: Method (), Parent:: Method () can be called.
The final keyword prevents the class from being inherited, and sometimes it just wants to be a separate class and not be inherited by other classes.

3. Abstract classes and methods
Abstract class Attributes:
* Abstract classes cannot produce instance objects, they can only be inherited;
* Abstract methods must be in abstract classes, abstract classes are not necessarily abstract methods;
* When inheriting an abstract class, subclasses must override all abstract methods in the parent class;
* methods defined as abstractions simply declare their invocation methods (parameters) and are not implemented.

Abstract class Computer {    abstract function run ();} Final class Notebookcomputer extends computer {public    function run () {        echo ' implementation of abstract class ';    }}

3. polymorphic
Polymorphism refers to the ability of OOP to redefine or change the nature or behavior of a class based on the context of the class being used, or to say that many different implementations of the interface are polymorphic.

interface computer {public function version (); Public function work ();}    Class Notebookcomputer implements computer {public Function version () {echo ' Lenovo <br> '; The Public function work () {echo ' notebook is being carried at any time!    ';    }}class Desktopcomputer implements computer {public Function version () {echo ' IBM '; The Public function work () {echo ' desktop computer is running on the workstation!    ';        }}class Person {public Function run ($type) {$type->version ();    $type->work (); }} $person = new person (), $desktopcomputer = new Desktopcomputer (); $notebookcomputer = new Notebookcomputer (); $person-&G T;run ($notebookcomputer); 
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.