PHP Learning object-oriented Courseware 1th/2 page _php Basics

Source: Internet
Author: User
Tags abstract arrays modifier
The main three attributes of an object
Object behavior: You can apply those actions to the object, turn on the light, turn off the light is behavior.
The shape of the object: When applied to those methods is how the object responds, color, size, and shape.
Object representation: The representation of an object is equivalent to an identity card, which distinguishes between the same behavior and the state.

Object-oriented model
Object-oriented concepts:
OOP (object-oriented programming) it makes its code simpler and easier to maintain and more resilient
What is a class:
A class is a collection of objects with the same attributes and services, such as people, books, ships, cars all belong to the class, he made a unified abstract description of objects belonging to the class, in programming language class is a separate program, it should have a class name including attribute description and service two parts.
What is an object:
An object is an entity that describes an objective event in a system, and he is a basic unit of the system. * Data and code are bundled in an entity *, an object consists of a set of properties and a set of behaviors that operate on the set of attributes.
From an abstract point of view, an object is an abstraction of something in the problem domain or implementation domain. He reflects the information and role that the thing holds in the system: it is a set of properties and a package that has permission to operate on these properties. The objective world is made up of the relationship between objects and objects.
Relationships between classes and objects:
The relationship between the class and the object is like the relationship between the mold and the casting, the result of the strength of the class is the object, and the abstraction of the object is the class, and the class describes the set of objects with the same attributes (attributes) and the same behavior.

Classes and properties and methods
Define class syntax format in PHP:
class classname [optional attribute]{
Public $property [=value];///To declare a common identity and give a variable variable can also be assigned a value

member functions in Methods of function functionname (args) {//Class
Code} ...
Method of a class (member function)
}
Build object (Instantiation of Class): $ object Name =new classname ();

Using the properties of an object
In a class, you can access a special pointer $this when the variable is set or accessed through an action in the class, referenced using $this->name.
Generation of objects
After the definition of a class with a new to declare that, because of the encapsulation characteristics of object data, objects can not be directly accessed by the main program block through the object to invoke the defined attributes and behavioral functions, indirectly to achieve access control class data.

The relationship between objects and classes
The relationship between objects and classes:
Objects are real and occupy dynamic resources.
A class is the blueprint of an object and may occupy static resources.
Object properties Occupy dynamic resources
A class (static) property is actually a "global variable" on a class namespace
Performance considerations:
Each object takes up data space alone
Increased call levels may consume execution time
Parameter form and transfer mode of the method
The parameters of a method can be basic data types, arrays, and class objects.
Basic data type: Value parameter Pass
Arrays: Value parameter passes
Class objects: Reference passing

Constructors
constructors function as initialization in a class
Constructors are generated in the same way as other functions except that their name must be __construct ().
Syntax format: function __construct (parameters) {
。。。。。。。。
}
Example:
Class person{
Public $name;
Public $sex;
Public $age;
function __construct ($name, $sex, $age) {
echo "I am a constructor <br>";
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
Output Result: Initialization
destructor
The system performs the destructor automatically when the object is detached from its scope (for example, the function where the object is already called). You should use freed memory in the destructor before exiting.
Destructors __destruct destructors do not have any arguments
Example: Class person{
function _ _destruct ()
{echo "Bye bye!"; }
}
$a =new person ();

Access type
Public common (public modifier) classes are accessible both inside and outside of the class
Private private modifiers can only be accessed within a class
Protected protected (protected member modifier) subclasses can access the outside class

Three important features of OOP
Encapsulation, inheritance, polymorphism
Encapsulation: Encapsulation is the combination of the object's attributes and behavior into a separate unit.
It takes two steps to encapsulate a class. The first step is to privatize a class the second step is to use set and get to make read assignment operations
His advantage is: hidden class implementation Details, you can easily add logic control, restricting the property of unreasonable operation, easy to modify the maintenance of enhanced code.

__get and __set
Generally speaking, the class of private words more in line with the logic of reality.
There are two predefined functions for obtaining and applying operations.
__get gets a value that is usually the value of a field
__set set value is usually the value of a field
When __call calls a method that does not exist in an object, it generates an error called () method to handle the situation.
Current 1/2 page 12 Next read the full text

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.