Three major features of php: encapsulation, inheritance, polymorphism-PHP source code

Source: Internet
Author: User
This article mainly introduces three major features of php: encapsulation, inheritance, and polymorphism. It has good reference value. let's take a look at it below. This article mainly introduces three major features of php: encapsulation, inheritance, and polymorphism. It has good reference value. let's take a look at it together with the small editor.

1. encapsulation

Objective: to make the class safer

Practice: if the member variable is private, the member variable is indirectly operated through the method and the restriction conditions are added to the method.

II. Inheritance

Concept: subclass can inherit everything in the parent class.

Method rewriting: override the method of the parent class in the subclass.

Feature: single inheritance: One subclass can have multiple parent classes, and one parent class can derive multiple child classes.

Override: method rewriting

Overload: overload, edit polymorphism

III. polymorphism (running polymorphism)

Concept: The parent class references a subclass instance. because the child class overwrites the method of the parent class, the parent class references different states when calling this method.

Condition:

1 must occur under integration

2. the parent class method must be overwritten.

3. the parent class references the call method.

If a method requires a parent class parameter, you can give a subclass object

Static

Common member

Common members belong to objects.

Static Member

Static members belong to the class

Keyword: static

Self keyword: indicates the class in the class

Common members cannot be called in static methods.

Static members can be called in common methods.

Interface

Extremely abstract class

The interface cannot contain Member Variables. it can only contain member methods.

Member methods can have no function bodies

Interface keyword: interface

The class that implements the interface. all methods in the interface must be implemented.

Loading Class:

include("./Ren.class.php");include "./Ren.class.php";require("./Ren.class.php");require "./Ren.class.php";require_once("./Ren.class.php");require_once "./Ren.class.php";

Automatic class loading method

All class files should be placed in the same directory.

The naming rules of all class files are consistent.

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.