PHP Learning Note--9. Object oriented

Source: Internet
Author: User

1. Overview

① Object-oriented (OOP)

Object-oriented programming is a computer programming architecture

The basic principle is that a computer program is a combination of a single unit or object that can act as a subroutine.

Three main goals to achieve: reusability, flexibility, and scalability

The three main features of object-oriented (encapsulation, inheritance, polymorphism) are indispensable

"Object-based" means that only encapsulated objects can be used, and new object types cannot be derived

② Classes and objects

Class describes a set of objects that have the same attributes (attributes) and the same behavior (methods); It provides a uniform abstract description of all objects that belong to the class

A class is a data type, and the most important difference between object-oriented and process-oriented is that you can customize the data type according to your requirements.

An object is an entity used to describe an objective thing in a system, which is a basic unit of a system.

③ Properties and methods

Properties--What is the class (noun), the ' variable '

Method----the class is capable of what (verb)--that is ' function '

2. Declaration and application

① Basic Syntax

Class Name {
Public $ property name [= ' property value '];
Public Function Method name () {
Echo $this Property Name/method name ();
}
}
$ object = new class name ();
$ object, property name [= ' new attribute value '];
$ object, method name ();

Attention:
The class name is usually capitalized in the first letter of the word, such as Actionclass
Property value must be a direct value and cannot be an expression or function return value
Inside a method, you can call the outside normal function of the class directly; external variables cannot be called

② Permission Modifiers

You can add public/protected/private to the property name/method name before modifying it;
If not added, the default is public mode

The Access/Modify permissions are:
Publicprotectedprivate
√√√ in this class
√√x in subclasses
External √x X

3. Three major features

① Package

Defined:
Hide the implementation details of the properties and methods, only the external public interface;
Read and modify access levels for properties/methods in the control program

Grammar:
Class Name {
Private $ property name [= ' property value '];
Private Function Method name () {
Method body;
}
Public Function common method name () {
method body;//manipulate private properties/methods
}
}
$ object = new class name ();
$ object, public method name ();

② inheritance

Defined:
A class takes a "one" class as the parent class
Further additions or modifications are made on the basis of inheriting all the properties and methods of the parent class

Grammar:
Class Subclass extents Parent class {
Properties/methods;
}

Attention:
The Public/protected method that accesses the parent class in the subclass, including the constructor method/static method, requires the parent:: Method name ();
The private method that accesses the parent class in a subclass is indirectly accessed by accessing the public method of the parent class.
Subclasses are allowed to be relaxed or remain unchanged when overriding the properties/methods of the parent class (override)

Overload (Overload):
Refers to the existence of multiple methods with the same name in a class (the parameter type or number is different);
When called, the corresponding method is called according to the parameters passed in.
A method with the same name is not allowed in PHP, but can be obtained through the Func_get_args () method.
Make a judgment to simulate an overloaded effect
Note: the Func_get_args () method returns an array type

③ polymorphism

Defined:
In a strongly typed language, the parameter of a method is the type at which it is declared, and must be the type when the argument is passed
In order to have a flexible feature, the parameter type is determined by the passed-in subclass parameter type.
While PHP is a weakly typed language, there is no restriction on the type, and any type of data can be used when the parameter is passed
Instead of making the procedure rigorous enough, a certain limit is needed to form a perfect polymorphic

Grammar:
Class Name {
Public Function Method Name (parent class $ variable) {
Method body;
}
}
Class Parent {//used to restrict types

}
Class Subclass 1 Extents parent class {
Public Function Subclass Method name () {
Method body;
}
}
Class Subclass 2 Extents parent class {
Public Function Subclass Method name () {
Method body;
}
}
$ object = new class name ();
$ object 1 = new subclass 1 ();
$ object 2 = new subclass 2 ();
$ object, method name ($ object 1);
$ object, method name ($ object 2); The same method has different effects due to the different parameters passed in.

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

PHP Learning Note--9. Object oriented

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.