Php object-oriented tutorial 3

Source: Internet
Author: User
Php object-oriented tutorial 3 5. how to instantiate an object
As we have mentioned above, the unit of the object-oriented program is the object, but the object is instantiated through the class.
It will be declared. The next step is to instantiate the object.
After the class is defined, we use the new keyword to generate an object.

$ Object name = new class name ();
 $ P1 = new Person ();

This code is the process of generating instance objects through classes. $ p1 is the object name of our instance. Similarly, $ p2, $ p3
It is also the object name of our instance. a class can be used to generate multiple objects. each object is independent.
When three people come out of the instance, there is no connection between them. it only means that they are all human beings and each person has his/her own surname.
Name, gender, and age. each person has a way to speak and walk, as long as it is a member attribute and a member in the class.
METHOD. The instantiated object contains these attributes and methods.
It is also a data type like integer and floating point type in PHP. it is used to store different types of data.
When all objects are loaded to the memory for use, how is the object reflected in the memory? The memory is logically
It can be divided into four sections: stack space segment, heap space segment, code segment, and initialization static segment. different declarations in the program are placed in different memory segments.
Inside, the stack space segment is the place where storage occupies the same space length and occupies a small amount of data, such as integer,
, And so on. The occupied space in the memory is of a length of 64-bit and 4-byte length. So data
The length is not long, and the data that occupies a lot of space is stored in that memory segment? Such data is stored in
Heap memory. Stack memory can be directly accessed, while Stack memory cannot be directly accessed. For our objects
A number is a type of big data type that occupies an indefinite amount of space. Therefore, objects are stored in the heap, but object names
Is placed in the stack, so that the object can be used through the object name.

$p1=new Person();

For details, see:
From this we can see that $ p1 = new Person (); the right side of the equal sign is a real object instance, the entity in the heap memory,
There are three new persons () in total, so three spaces will be created in the heap to generate three instance objects, each of which is a phase
They are independent of each other and use their own space. in PHP, if there is a new keyword, a pair will be instantiated.
In the heap.
Each instance object in the heap is stored as a property. for example, the instance object in the heap contains a name and a property.
Age. Each attribute has an address.
$ P1 = new Person (); $ p1 on the right of the equal sign is a reference variable. The first address of the object is assigned by the assign operator "= ".
Reference the variable "$ p1", so $ p1 is the variable that stores the first address of the object, $ p1 is placed in the stack memory, and $ p1 is equivalent to
Pointers point to the objects in the heap, so we can use the reference variable $ p1 to operate the object, which is also called Object reference.
Is an object.
6. how to use members in an object
The above shows that the PHP object has two types of members: Member attributes and member methods. Object we can declare
Now, $ p1 = new Person (); how do I use object members? To access members of an object, you must use a special operation.
"->" To complete access to object members:
Object-> attribute $ p1-> name; $ p2-> age; $ p3-> sex;
Object-> method $ p1-> say (); $ p2-> run ();
For example:

 Name = "zhang san"; $ p1-> sex = "male"; $ p1-> age = 20; // the following three rows are the attributes of $ p1 object access echo "p1 object name :". $ p1-> name."
"; Echo" the gender of the p1 object is: ". $ p1-> sex ."
"; Echo" the age of the p1 object is: ". $ p1-> age ."
"; // The following two rows access the method $ p1-> say (); $ p1-> run () in the $ p1 object (); // the following three rows assign $ p2 object attributes $ p2-> name = "Li Si"; $ p2-> sex = "female"; $ p2-> age = 30; // the following three rows show the attribute echo "p2 object name:" Accessing $ p2 objects :". $ p2-> name."
"; Echo" p2 object gender: ". $ p2-> sex ."
"; Echo" p2 object age: ". $ p2-> age ."
"; // The following two rows access the $ p2 object method $ p2-> say (); $ p2-> run (); // the following three rows assign $ p3 object attributes $ p3-> name = ""; $ p3-> sex = "male"; $ p3-> age = 40; // the following three rows show the attribute echo "p3 object name:" Accessing $ p3 object :". $ p3-> name."
"; Echo" the sex of the p3 object is: ". $ p3-> sex ."
"; LAMP lecture hall PHP object-oriented technology (comprehensive explanation) echo" p3 object age: ". $ p3-> age ."
"; // The following two rows access the $ p3 object's method $ p3-> say (); $ p3-> run ();?>

From the above example, we can see that only the members in the object must access the object in the form of object-> attribute, object-> method.
You can use either of the following methods to access the members of an object:

The above is the content of php object-oriented tutorial 3. For more information, see PHP Chinese network (www.php1.cn )!

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.