PHP object-oriented Technology (full explanation) (Gaulo)
This section includes the following technical points altogether:
1. The concept of a face image object
2. What is class, what is object, class and object this relationship
3. What is Object oriented programming?
4. How to abstract a class.
5. How to instantiate an object
6. How to use the members of the object
7. Use of special references to "$this"
8. Construction method and the method of destructor
9. Encapsulation of
10.__set () __get () __isset () __unset ()--application of four methods
11. Class of the following letter
13. Overload the New method
14. Type of Access
15. Static members and the use of constants in classes
Application of 16.final Keywords
17.__tostring () method
18. Cloning objects
19.__call Processing Call Error
20. Auto Load Class
21. Serialization of objects
22. Abstract methods and abstract classes
23.PHP5 Interface Technology
12. Multi-state application
1. The concept of a face image object
Object-oriented programming (Object Oriented programming, OOP, object-oriented programming) is a computer programming architecture, the basic principle of OOP is that computer programs are composed of a single unit or object that can act as a subroutine. OOP achieves the three goals of software engineering: reusability, flexibility, and scalability. To achieve the overall operation, each object is able to receive information, process data, and send information to other objects. Object-oriented has always been a popular topic in the field of software development, first of all, object-oriented accords with the general law of human beings. Secondly, the application of object-oriented method can make each part of the system perform their duties and dens. Open a door for programmers to make their programming code simpler, easier to maintain, and more reusable. Some people say that PHP is not a real object-oriented language, which is true. PHP is a hybrid language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you might want to use pure OOP in PHP to declare classes, and only objects and classes in your project. This concept I do not say much, because there are many people friends away from object-oriented programming is the main reason is a touch-oriented concept of the time to understand, so do not want to learn. Wait for the reader to read the whole content before you understand the concept. 2. What is class, what is object, class and object this relationship
Class is the concept of a class that is a collection of objects with the same properties and services. It provides a unified abstract description of all objects belonging to the class, including the two main parts of the property and service. In an object-oriented programming language, a class is a separate program unit that should have a class name and include a description of the property and two main parts of the description of the service.
Object concept: An object is an entity that is used to describe objective things in the system, which is a basic unit of the system. An object consists of a set of properties and a set of services that operate on this set of properties. In a more abstract sense, an object is an abstraction of the problem domain or of something in the implementation domain that reflects the information and the role that the thing needs to be saved in the system; it is a package of properties and a set of services that are authorized to operate on these properties. The objective world is made up of the relationship between objects and objects.
The relationship between a class and an object is like the relationship between a mold and a casting, and the instantiation of a class is an object, and the abstraction of a class of objects is the classes. Class describes an object that has the same attributes (attributes) and the same behavior (method).
It's probably their definition, maybe you're just a friend of the object don't be confused by the concept of things, give you a column, if you want to go to Zhongguancun to buy a few assembled PC machine, where you first step to do, is not installed engineers and you sit together, According to the information you have provided with you to complete a installed configuration list Ah, this configuration can be imagined as a class, it is a piece of paper, but it is recorded on the PC you want to buy information, if you use this configuration to buy 10 machines, then the 10 machine, are based on this configuration list composed of, So the 10 machines are a type, or they can be said to be a class. Then what is the object, the class of the instantiation result is the object, with this configuration form (instantiated) of the machine is the object, we can operate the entity, 10 machines, 10 objects. Each machine is independent, only to show that they are the same class, do any action on one of the machines will not affect the other 9 machines, but I modify the class, that is, add one or fewer accessories to the configuration list, then the 9 machines are changed, this is the relationship between class and object (the instantiation result of the class is the object) .
3. What is Object oriented programming?
Not to mention his concept, if you want to set up a computer classroom, the first to have a room, the room must have n computers, there are n tables, n chairs, whiteboard, projectors and so on, these are what, just we said, this is the object, can see the entity, Can say this computer classroom unit is this one entity object, they together compose this computer classroom, then we are to do the procedure, this and object-oriented have what relation. Develop a system program and build a computer classroom like, you abstract each independent function module into a class to form objects, composed of multiple objects of the system, these objects are able to receive information, processing data and send information to other objects and so on interaction. constitutes an object-oriented program.
4. How to abstract a class.
I've already covered it. Object-oriented program unit is the object, but the object is through the class instantiation, so the first thing we have to do is how to declare the class, make a class is very easy, as long as the master of the Basic program syntax to define the rules can be done, so the difficulty is there. How many classes to use for a project, how many objects, where to define the class, what class to define, how many objects the class has, how many attributes there are in the class, how many methods, and so on, require the reader to design and summarize the actual problems in actual development.
The definition of the class:
Class Name {
}
Using a keyword class and the following plus a class name you want, plus a pair of curly braces, the structure of a class is defined, as long as you write the code inside it, but what it says. What to write. How to write is a complete class. Above, the use of classes is to let it instance of the object to give us, it is necessary to know what kind of object you want, like the above we speak of a set of the list of what to write, you installed the machine is what. For example, a person is an object, how do you recommend a person you are optimistic about the leadership of you. Of course, the more detailed the better:
First, you will introduce the person's name, sex, age, height, weight, phone number, home address, and so on.
Then you have to introduce what this person can do, drive, speak English, use a computer, and so on.
As long as you introduce a little more, others to this person a little bit more understanding, this is what we describe to a person, and now we conclude that all of the objects we describe are similar to each other, from the description of the above, to make a class that is divided into two parts from a defined point of view, the first of which is statically described, The second is the dynamic description, static description is what we call attributes, as we see above, people's name, sex, age, height, weight, telephone, home address and so on. Dynamic is the function of this object, such as the person can drive, speak English, can use computers and so on, abstract into the program, we put the dynamic written function or method, functions and methods are the same. So, all classes are written from both the attribute and the method, and the attribute is called the member property of the class, and the method is called the member method of the class.
Class Person {
Member properties: Name, sex, age, height, weight, telephone number, home address
Member method: Can drive, speak English, can use the computer
}
Property:
Declare a variable by using the keyword "var" in the class definition. That is, the property of the class is created, although the initial value can be given when declaring the member property, but it is not necessary to give the member property the first values when declaring the class, for example, if you assign a person's name to "John", then use the class instance for dozens of people, These dozens of people are called John, so there is no need, we are in the instance after the object to the member property to the first value.
such as: Var $somevar;
Method (member function):
By declaring a function in the class definition, you create a method of the class.
such as: function somefun (parameter list)
{ ... ... }
<?php
Class Person
{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
The following is a method of member of the person
function say ()//The way this person can speak
{
echo "This man is talking";
}
function run ()//The way this person can walk
{
echo "This man is walking";
}
}
?>
The above is a class declaration, a class declared from a property and a method, but member properties are best not to give a value at the time of declaration, because the person we are doing is a descriptive information that will be used to instantiate the object in the future, such as instantiating 10 objects, then the 10, each person's name, gender, Age is not the same, so it is best not to assign an initial value to a member attribute in this place, but to assign values to each object separately.
The same way you can make the class you want, as long as you can use the attributes and methods can be described by the entity can be defined as a class, to instantiate the object.
In order to strengthen your understanding of the class, we do a class, do a shape of the class, the shape of a wide range of points, we make a rectangle, first analysis, think about the two aspects of the properties of the rectangle have what. What the function of a rectangle is.
Class Rectangle
{
The properties of the rectangle
The length of the rectangle;
The width of the rectangle;
The method of the rectangle
The circumference of the rectangle;
The area of the rectangle;
}
<?php
Class Rect
{
var $kuan;
var $gao;
function Zhouchang ()
{
Compute the perimeter of the rectangle;
}
function Mianji ()
{
Compute the area of the rectangle;
}
}
?>
If you use this class to create multiple rectangular objects, each rectangle has its own length and width, and you can find its perimeter and area.
class declaration Let's just get here.
5. How to instantiate an object
As we said above, the object of the Object-oriented program is the object, but the object is instantiated from the class, and now that our class declares, the next step is to instantiate the object.
When the class is defined, we use the New keyword to generate an object.
$ Object name = new class name ();
<?php
Class Person
{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
The following is a method of member of the person
function say ()//The way this person can speak
{
echo "This man is talking";
}
function run ()//The way this person can walk
{
echo "This man is walking";
}
}
$p 1=new person ();
$p 2=new person ();
$p 3=new person ();
?>
$p 1=new person ();
This code is the process of generating an instance object from a class, $p 1 is our instance of the object name, similarly, $p 2, $p 3 is also our instance of the object name, a class can be instances of multiple objects, each object is independent, the above code equivalent to the example of 3 people come out, There is no connection between everyone, can only show that they are human, everyone has their own name, gender and age attributes, everyone has to talk and walk the way, as long as it is reflected in the class member properties and member methods, instantiated objects contained in these attributes and methods.
Like in PHP and integral type, floating-point type, but also a data class, are stored in different types of data, in the runtime to be loaded into memory to use, then the object in memory how to embody it. Memory from the ROM is basically divided into 4 segments, stack space segment, heap space segment, code snippet, the initial static section, the program inside the different declarations in different memory segments, stack space segment is to occupy the same space length and occupy small space of data types, such as Integer 1, 10, 100, 1000, 10000, 100000 and so on, in the memory occupies space is equal length, are 64 bits 4 bytes. So the data length is indefinite, and the data type that occupies a large space is placed in that segment of memory. Such data is placed in the heap memory. Stack memory can be accessed directly, while heap memory is not directly accessible to memory. The number of our objects is a large data type and occupy the indefinite length of the type, so that the object is placed in the heap, but the object name is placed in the stack, so that the object name can use the object.
$p 1=new person ();
For this piece of code, $p 1 is the object name in the stack memory, the new person () is the real object in the heap memory, see the following figure:
The $p1=new person () can be seen from the above figure. The equal sign right is the real object instance, the entity inside the heap memory, there are 3 new person () in the graph, so it will open up 3 spaces in the heap, produce 3 instance objects, each object is independent each other, use own space, In PHP, as long as there is a new keyword will be instantiated out of an object, in the heap to open up a piece of their own space.
Each instance object in the heap is a storage attribute, for example, now the instance object inside the heap contains the name, gender, and age. Each attribute also has an address.
$p 1=new person (); the right $p1 of the equal sign is a reference variable, assigning the first address of an object to the "$p 1" reference variable by the assignment operator "=", so $P1 is the variable that stores the first address of the object, $p 1 is placed inside the stack memory, $p 1 is equivalent to a pointer to an object inside the heap, So we can manipulate objects by $P1 This reference variable, and we usually call object references as objects.
6. How to use the members of the object
There are two types of members in the PHP object that are member properties and the member method. Object we may declare, $p 1=new person (); How to use the members of the object? To access members of an object, you use a special operator "->" to complete the access of the object member:
Object-> Property $p 1->name; $p 2->age; $p 3->sex;
Object-> method $p 1->say (); $p 2->run ();
As in the following example:
<?php
Class Person
{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
The following is a method of member of the person
function say ()//The way this person can speak
{
echo "This man is talking";
}
function run ()//The way this person can walk
{
echo "This man is walking";
}
}
$p 1=new person (); Create Instance Object $p1
$p 2=new person (); Create Instance Object $p2
$p 3=new person (); Create Instance Object $p3
The next three lines are assigning values to the $p1 object property
$p 1->name= "John";
$p 1->sex= "Male";
$p 1->age=20;
The following three lines are the properties that access the $p1 object
The name of the echo "P1 object is:". $p 1->name. " <br> ";
The gender of the echo "P1 object" is: ". $p 1->sex." <br> ";
The age of the echo "P1 object" is: ". $p 1->age." <br> ";
The following two lines access methods in the $p1 object
$p 1->say ();
$p 1->run ();
The next three lines are assigning values to the $p2 object property
$p 2->name= "Dick";
$p 2->sex= "female";
$p 2->age=30;
The following three lines are the properties that access the $p2 object
The name of the echo "P2 object is:". $p 2->name. " <br> ";
The gender of the echo "P2 object" is: ". $p 2->sex." <br> ";
The age of the echo "P2 object" is: ". $p 2->age." <br> ";
The following two lines access methods in the $p2 object