PHP Object-Oriented programming

Source: Internet
Author: User

Object-oriented three major features:

Encapsulation, inheritance, polymorphism

Class:

Class (Properties, methods), objects, method overrides,

Static (property/method) Static members are shared by each instance object of this class and can be accessed with self

Defining constants in a class uses the keyword "Const", which is accessed in the same way as a member property decorated with "const" and accessed by members of the "static" modifier, using the "self" keyword in the method, using the "Class name". However, you do not have to use the "$" symbol and you cannot use objects to access it.

Abstract class

Abstract method, the so-called no method body refers to the method declaration when there is no curly braces and the contents of it, but directly at the declaration of the method name after the end of the semicolon, in addition to the Declaration of abstract methods also add a keyword "abstract" to decorate;

As long as there is one method in a class that is abstract, the class is defined as an abstract class, and the abstract class is modified using the "abstract" keyword;

An abstract class cannot produce an instance object

Subclasses must implement all the abstract methods in the parent class, otherwise there is an abstract method in the subclass, then the subclass is an abstract class, or the class cannot be instantiated;

Interface

Like most object-oriented programming languages, PHP does not support multiple inheritance. This means that each class can inherit only one parent class. To solve this problem, PHP introduced the interface, the idea of the interface is to specify a implementation of the interface of the class must implement a series of methods. Interface is a special kind of abstract class, abstract class is a special class, so the interface is a special class, why is the interface is a special kind of abstract class? If all of the methods in an abstract class are abstract, then we use the "interface" in a different way, that is, all the methods in the interface must be declared as abstract methods, and the interface cannot declare variables (but can declare constant constant). And all the members of the interface are public permissions. So the subclass must also use the public permission limit when implementing it.

Declare a class when we use the keyword is "class", and the interface of a special class, the use of the keyword is "interface";

Because the interface is a special kind of abstract class, all of the methods are abstract methods, so the interface can not produce an instance object; It is also done as a specification, and all abstract methods require subclasses to be implemented.

We can use the "extends" keyword to get an interface to inherit another interface

This abstract class also has subclasses that implement all of its abstract methods;

PHP Object-Oriented programming

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.