PHP Object-oriented features

Source: Internet
Author: User

1) Inheritance

keyword:extends (subclass extends parent class)

Purpose: Reduce the amount of code duplication ( inheritance can greatly reduce duplication in our code )

Inheritance characteristics:

Default Parent class the properties and behaviors in Animal are inherited by the quilt class, so you don't have to declare it again .

PHP is a single inheritance , a class can inherit only one parent class, but a parent class can be inherited by multiple subclasses.

Subclasses cannot inherit private properties and private methods of the parent class.

Methods of classes in PHP5 can be inherited, and constructors of classes can be inherited

Inheritance can only have and only one parent class

Parent: This keyword, meaning parent, refers here to the parent class object of the current class

(2) package

① Definition: Encapsulation is the combination of the object's properties and behavior into a separate unit, and as far as possible to conceal the internal details of the object.

② Advantages: Information concealment, encapsulation is hidden

③ encapsulation principle: In the software, the requirements of the object beyond the arbitrary access to the object's internal data (attributes), so as to effectively avoid the external error on its "cross-infection", so that software errors can be localized, greatly reduce the difficulty of error-checking and wrong.

④ Purpose:

Hide implementation details of a class

To prevent unauthorized access to encapsulated data, the user can only access the data by implementing a custom good method, it is convenient to join the control logic and restrict the unreasonable operation of the attribute.

Helps ensure the integrity of data

facilitates the protection of data

(3) polymorphic

① Abstract: It is impossible to explain exactly, but there is a certain concept or name

Abstract classes: Classes that are decorated with abstract keywords are called abstract classes.

Keywords:abstract

Characteristics:

Abstract classes cannot be instantiated

Abstract classes do not necessarily need to have abstract methods, but classes that contain abstract methods must be abstract classes

Abstract method:

Abstract class Animal

{
Public Abstract function shout ();
}

A function method that uses the abstract modifier is called an abstract method

Abstract methods only the declaration part of a function, which does not contain the implementation part, and is to be terminated with a semicolon

A class that inherits an abstract method must implement the definition of an abstract method

② interface

Interfaces are collections of abstract methods and static constant definitions

An interface is a special kind of abstract class that contains only abstract methods and static constants, and no other type

Note: The abstract method in an interface can only be public, and the default is public. It can't be protected and private .

Interfaces are often a collection of functions

Interface Ifly

{
Public function fly ();
}

functions in interfaces are public abstract function methods that cannot add the abstract keyword, and can also omit the

Implementation interface: implements

Implement an interface, you must implement all the abstract methods in the interface

A class can implement multiple interfaces

PHP Object-oriented features

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.