Php learning object-oriented courseware page 1/2 _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php learning object-oriented courseware 12th page. Three main properties of an object: actions that can be performed on the object. turning on the light is the action. Object form: three main features of an object, including how the object responds, colors, and objects, are applied.
Object Behavior: you can perform operations on the object. if you turn on the light, turning off the light is behavior.
Object form: how the object responds, colors, dimensions, and shapes are applied.
Object representation: the representation of an object is equivalent to an ID card, which specifically distinguishes between the same behavior and status.

Object-oriented model
Object-oriented concepts:
Oop (object-oriented programming) can make its code simpler and easier to maintain and has stronger reusability.
What is a class:
A class is a collection of objects with the same attributes and services, such as people, books, ships, and vehicles. It provides a unified abstract description for objects of this class, in programming languages, a class is a separate program. it should have a class name that includes two parts: attribute description and service.
What is an object:
An object is an entity that describes objective events in the system. it is a basic unit of the system. * Data and code are bundled into one entity. * An object consists of a group of attributes and a group of actions that operate on these attributes.
From an abstract point of view, an object is an abstraction of a problem domain or something in the domain. It reflects the information stored in the system and its role: it is a set of attributes and an encapsulation body that has the right to operate on these attributes. The objective world is composed of links between objects.
Relationships between classes and objects:
The relationship between a class and an object is like the relationship between a mold and a casting. the result of the strength of a class is an object, and the abstraction of an object is a class, class describes a group of objects with the same properties and behaviors.

Classes and attributes and methods
Syntax format for defining classes in PHP:
Class classname [optional attributes] {
Public $ property [= value];... // declare a public ID with public and assign a value to the variable

Function functionname (args) {// member function in the class method
Code }...
// Class method (member function)
}
Generate an object (class instantiation): $ object name = new classname ();

Use object attributes
In a class, you can access a special pointer $ this. when this class is set or accessed through an operation, $ this-> name is used for reference.
Object generation
Define a class and declare it with a new one. because of the encapsulation characteristics of object data, objects that cannot be directly accessed by the main program block must call the attributes and behavior functions defined in the class through the object to indirectly achieve the purpose of accessing the data in the control class.

Relationship between objects and classes
Relationship between objects and classes:
An object actually exists and occupies dynamic resources.
A class is the blueprint of an object and may occupy static resources.
Object attributes occupy dynamic resources
The class (static) attribute is actually a "global variable" in the class namespace"
Performance considerations:
Each object occupies data space separately.
The added invocation level may consume execution time.
Method parameter form and transmission method
Method parameters can be basic data types, arrays, and class objects.
Basic data type: passing value parameters
Array: passing value parameters
Class object: reference transfer

Constructor
Constructor is used for class initialization.
Like other functions, the constructor must be named _ construct ().
Syntax format: function _ construct (parameter ){
........
}
Example:
Class Person {
Public $ name;
Public $ sex;
Public $ age;
Function _ construct ($ name, $ sex, $ age ){
Echo "I'm a constructor
";
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
Output result: Initialization
Destructor
When an object is out of its scope (for example, the function of the object has been called), the system automatically executes the destructor. Memory should be released in the destructor before exiting.
The Destructor _ destruct does not have any parameters.
Example: class person {
Function _ destruct ()
{Echo "bye! ";}
}
$ A = new person ();

Access type
Public (public modifier) class can be accessed both inside and outside the class
Private (private modifier) can only be accessed within the class
The protected (protected member modifier) subclass of protected can access the outside of the class.

Three important features of oop
Encapsulation, inheritance, and polymorphism
Encapsulation: encapsulation combines object attributes and behaviors into an independent unit.
Two steps are required to encapsulate a class. The first step is to privatize a class. The second step is to use the set and get operations to read and assign values.
Its advantage is: hiding the implementation details of the class can facilitate logical control, restrict unreasonable operations on the attribute, and facilitate modification and enhance the maintainability of the code.

_ Get and _ set
Generally, class private words are more in line with the actual logic.
Predefined functions are used to obtain and apply values.
_ Get is usually the value of the domain.
_ Set is usually the value of the field.
_ Call when calling a method that does not exist in an object, an error call () is generated to handle this situation.

Actions of the objects: operations that can be performed on objects. turning on the light is an action. Object form: when applying how the object responds, the color ,...

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.