Chapter 2 Object-oriented basics _ PHP Tutorial

Source: Internet
Author: User
Tags abstract definition
Chapter 4 object-oriented basics. Learning points: 1. what is object-oriented 2. features of OOP 3. key OOP concepts 4. many languages for creating OOP are Object-Oriented (OOP), and PHP has introduced such features for years. Learning points:
1. what is object-oriented
2. features of OOP
3. key OOP concepts
4. create OOP

Many languages are Object-Oriented (OOP), and PHP has introduced such features for several years. Object-oriented
The birth of an image is a major change in the development paradigm. the focus of programming is to return to its data from the logic of the application.
. In other words, OOP shifts the focus from procedural events of programming to real entities of final modeling. This makes the application process
The sequence is closer to the real world around us.

1. what is object-oriented
Process-oriented
This is like you are an employee in the company. today we have a task to assemble a batch of computers in the company. Then you start to collect
Purchase, bargain, transport back to the company, start to assemble, wiring the network, debug the machine, complete. That is to say, process-oriented
It is a concrete implementation with clear details.
Object-oriented
It seems like you are the president of the company. you have assigned an employee a task of assembling a batch of computers. Complete. That is
In other words, object-oriented is abstract execution, which is done by the employee. In terms of details, the president does not need to go
Consider. This advantage is obvious. a company with a management level can coordinate jobs, but not a company with a management level.
Division, only some specific implementation of the function of the staff, will be a mess.

II. features of OOP
Encapsulation
Hide the attributes and implementation details of an object. only open interfaces are provided to control the read and modify access level of attributes in a program.
Combine abstract data with behaviors (or functions) to form an organic whole, that is, to integrate data
The source code of the operation data is organically combined to form a "class", where data and functions are members of the class.


Inheritance
Inheritance is a mechanism for obtaining one or more classes from a base class.
A class that inherits from another class is called a subclass of this class. This relationship is typically compared with a father or a child. Subclass will follow
Features of the parent class. These features consist of attributes and methods. Sub-classes can add new features outside the parent class, so sub-classes also
It is called the "extension" of the parent class ".

Polymorphism

Polymorphism means that OOP can redefine or change the nature or behavior of a class based on the context of the class used, or
There are many different implementations of the port, namely, polymorphism. Different subclass objects can be treated as parent classes to shield different subclasses.
Differences between objects, write common code, and make general programming to adapt to the changing needs.

III. key OOP concepts
Class)
Class is the definition of an object. It contains information about the object action method, including its name, method, and attribute.
And events. In fact, it is not an object because it does not exist in the memory. When the code of the referenced class is run
A new instance, that is, an object, is created in the memory. Although there is only one class, it can be created from this class in the memory
Multiple objects of the same type.
Object)
An object is a thing, an entity, a noun, and something that can be obtained. you can imagine any one that has its own logo.
Things. Objects are class instantiation. Some objects are active, and some objects are not.
For example, this car, this man, this House, this table, this plant, this check, this raincoat. Overview
Including: everything is an object.
For example, a class is an abstract definition of an object. to put it bluntly, if the object is a computer, the class can create many objects,
Class can generate a lot of computers, a little more white, class can be used as a computer production factory, can generate a lot of computers.
Field (filed)
A field is used to describe the nature of a certain aspect of the class. it is very similar to a common PHP variable, but it has some nuances.
.
For example: computer brand, computer model, and other features.
Attribute)
You can use methods to access and operate fields. On the one hand, you can protect fields and allow the same access to public fields.
Data.
For example, obtain the computer brand and set the computer brand.
Method)
Methods are very similar to functions, except that methods are used to define the behavior of classes. Same as a function, the method can accept the input
You can return a value to the caller.
For example, open the computer, enter the text, and run the program.

4. create OOP
Class creation:

Class Computer {// class field (member) // class method}

Object declaration:

$computer = new Computer();

The new identifier is used to create an object (instance) in the internal test, and the Computer () is generated by that class.
.
$ Computer is a variable and a reference to the generated instance.
Sometimes, you may need to create multiple objects.

$computer2 = new Computer();

You can use the var_dump () function to print information about a variable.

Add a field (member:

Class Computer {// class field (member) public $ _ name = 'Lenovo 100'; public $ _ model = 'lx ';}

1. declaration field:

Public $ _ name = 'Lenovo 123'

A) public is a modifier, indicating that this is a public field and can be accessed externally.
B) $ _ name is the variable name.
C) 'Lenovo 120 'is the variable value.
2. read fields

echo $computer->_name;

3. set fields

$computer->_name = 'IBM110';

Method creation

Class Computer {// class method function run () {echo 'I have run it successfully! ';}}

Execution method

$computer->run();

You can also add some parameters in the method and input these parameters during execution.

Class Computer {// function run ($ _ what) {echo $ _ what. 'of the class method is successfully run! ';}}$ Computer = new Computer (); $ computer-> run ('computer ');

Constructor
The so-called constructor is also a method, but a special method. The method name must be the same as the class name,
In addition, you do not need to call a method to execute it. you only need to instantiate it to complete the call process.
In general, constructor is always doing initialization.

Class Computer {// Constructor function Computer () {echo 'I am constructor! ';}} New Computer (); // The call is completed.

In PHP5, we can use the built-in _ construct method to identify the constructor, instead of having
The class name is the same.

Class Computer {// Constructor function _ construct () {echo 'I am the constructor! ';}}

The corresponding constructor method is also a built-in method, which is used after the entire class is used.
Run. It can be used to clear objects in the memory (it is automatically cleared when the script is executed ). If there is a script
After the rows are completed, they are not cleared, such as database data. it is necessary to use the destructor.

Class Computer {// destructor function _ destruct () {echo 'I am the destructor ';}}

Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.

Lifecycle 1. what is object-oriented 2.OOP 3. key OOP concepts 4. creating OOP many languages itself is object-oriented (OOP), and PHP has introduced this kind of feature for years...

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.