PHP object-oriented basic Note 1

Source: Internet
Author: User
Difficulties: Learn object-oriented ideas and syntaxes and understand ideas. PHP5 has added all basic object-oriented syntax to PHP5 compared to 4. I. object-Oriented programming ideology ObjectOriented (1) Event-centric programming ideology (2) Analysis of the steps required to solve the problem (3) implementation using functions (4) call when using II. what

Difficulties: Learn object-oriented ideas and syntaxes and understand ideas. PHP5 has added all basic object-oriented syntax to PHP5 compared to 4. I. object-Oriented programming ideology Object Oriented (1) Event-centric programming ideology (2) Analysis of the steps required to solve the problem (3) implementation using functions (4) call when using II. what

Difficulties: Learn object-oriented ideas and syntaxes and understand ideas.

PHP5 has added all basic object-oriented syntax to PHP5 compared to 4.

1. object-oriented programming ideology Object Oriented

(1) Event-centric Programming

(2) analyze the steps required to solve the problem

(3) Use functions to implement the steps

(4) called during use

2. What is an object?

(1) object

Represents specific things and abstract rules, plans, and events.

(2) Status and Behavior

Use data values to describe the status.

An action is an action of an object. It is used to change the state of an object.

Objects can be combined with data and operations to encapsulate data and objects in objects.

3. What is object-oriented

Object-oriented is a matter of functional division, not a step.

Focus on objects and the use of objects

Only object-oriented programs can truly conform to human thinking logic.

4. important advantages of object-oriented

Readability, reusability, stability, maintainability, and testability.

V. Relationship between classes and objects

The object is generated by class description. The attributes of an object are written in the class.

Each object in the same class is independent of each other.

Vi. Class Declaration

1. Simple format:

[Modifier] class name {// use the class keyword to add a space and the class name

[Member attributes] // also called member variables

[Member method] // also called a member function

}

2. Complete format:

[Modifier] class name [extends parent class] [implements interface 1], [interface 2]... {

[Member attributes] // also called member variables

[Member method] // also called a member function

}

3. Format

Modifier $ variable name [= default value]; // For example: public $ name = "zhangsan ";

Note: The member attributes cannot be expressions with operators, variables, methods, or function calls.



Correct definition method:



Common attribute modifiers: public, private, protected, static, and var (outdated)

I. Member Methods

1. Member method format:

[Modifier] function Method Name (parameter ..){

[Method body]

[Return value]

}

2. modifiers: public, protected, private, static, abstract, final

3. the declared member methods must be related to objects and cannot be meaningless operations.

Complete case:


8. Encapsulation

Encapsulation is to add modifiers to the member attributes and member methods of the object to hide the internal details of the object as much as possible to achieve access control for members (remember not to deny access)


Case:




9. Magic Methods

1. _ get ()

Automatic Call: it is automatically called when a private member is directly accessed!

If it is not a private attribute, you can pass the attribute name to the parameter.





In this way, each attribute returns a name and the output is as follows:



Or:



The output is as follows:




2. _ set ()

Automatic Call: two parameters are used when private attributes are directly set. One property name and one property value.




Output result:



3. _ isset ()

Automatic Call: When issset () is used to determine whether a private property exists, the _ isset () magic method is automatically called, and the parameter is the property name.

4. _ unset ()

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.