PHP Learning PHP Learning Object-oriented courseware 1th/2 page

Source: Internet
Author: User
Main three attributes of an object
Object behavior: You can apply those actions to objects, turn on the lights, turn off the lights is the behavior.
The shape of the object: When applying those methods is how the object responds, color, size, shape.
Object representation: The representation of an object is equivalent to an identity card, specifically distinguishing between the same behavior and the state of what is different.
Object-oriented model
Object-oriented concepts:
OOP (object-oriented programming) it could be that its code is simpler, easier to maintain and more robust.
What is a class:
A class is a collection of objects that have the same properties and services for example, people, books, ships, cars belong to the class, he made a uniform abstract description of the objects belonging to the class, in the programming language of the class is a separate program, it should have a class name including the description of the property and the service two parts.
What is an object:
An object is an entity that describes an objective event in the system, which 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 manipulate this set of properties.
From an abstract point of view, an object is an abstraction of a problem domain or some thing in the implementation domain. He reflects the information that the thing holds in the system and the role it plays: It is a set of attributes and a wrapper that has permission to manipulate these properties. The objective world is made up of the connections between objects and objects.
Class and object relationships:
The relationship between a class and an object is like the relationship between a mold and a casting, and the result of the strength of the class is the object, and the abstract of the object is the class, which describes a set of objects that have the same attributes (attributes) and the same behavior.
Classes and properties and methods
The class syntax format is defined in PHP:
class classname [optional attribute]{
Public $property [=value]; ...//Declare a common identity with a, and then give a variable variable can also assign a value
function functionname (args) {//member functions in Methods of class
Code} ...
Method of the 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 you set or access the variable by an action in the class, use $this->name to refer to it.
Generation of objects
After defining a class with a new to declare, because of the encapsulation characteristics of object data, object can not be accessed directly by the main program block by the object to invoke the properties and behavior functions defined in the class, and indirectly to achieve the purpose of accessing the data in the control class.
Relationship of objects and classes
Relationship of objects and classes:
objects are actually present and occupy dynamic resources.
A class is the blueprint for an object and may occupy a static resource.
Object properties Occupy dynamic resources
A class (static) property is actually a "global variable" on a class namespace
Performance considerations:
Each object consumes data space alone
Increased call hierarchy may consume execution time
Parameter form and delivery mode of the method
The parameters of a method can be basic data types, arrays, and class objects.
Basic data type: Value parameter delivery
Array: Value parameter Pass
Class object: Reference delivery
constructor function
Constructors are functions that initialize in a class
Constructors are generated in the same way as other functions except that their names must be __construct ().
Syntax format: function __construct (parameter) {
。。。。。。。。
}
Example:
Class person{
Public $name;
Public $sex;
Public $age;
function __construct ($name, $sex, $age) {
echo "I am a constructor
";
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
Output Result: Initialization
Destructors
The destructor is automatically executed when the object is detached from its scope (for example, the function where the object is already called). The memory should be freed in the destructor before exiting.
Destructors __destruct destructors do not have any parameters
Example: Class person{
function _ _destruct ()
{echo "Bye bye!";}
}
$a =new person ();
Type of Access
Public common (public modifier) class inside and outside of the class can be accessed
Private (private modifier) can only be accessed within the class
Protected protected (protected member modifier) subclasses can access classes outside of the class that cannot be accessed
Three key features of OOP
Encapsulation, inheritance, polymorphism
Encapsulation: Encapsulation is the combination of the object's properties and behavior into a separate unit.
Encapsulating a class requires two steps 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: hide the implementation details of the class, you can easily add logic control, restrict the unreasonable operation of the attribute, easy to modify the maintainability of the enhanced code.
__get and __set
It is generally said that the private words of the class more in line with the logic of reality.
Two functions are predefined to obtain and apply the value of the operation.
__get gets a value that is usually the value of a field
__set setting value is usually the value of the field
When __call calls a method that does not exist in an object, an error call () is generated to handle the situation.

Current 1/2 Page 12 next page

The above introduces PHP learning PHP Learning Object-oriented courseware 1th/2 page, including the content of PHP learning, I hope to be interested in PHP tutorial friends helpful.

  • 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.