PHP object-oriented (ii) Instantiating objects using Object Members _php Foundation

Source: Internet
Author: User
Tags object object php and
5. How to instantiate an object
As we said above, the object object is the unit of the Object-oriented program, but the object is also instantiated by the class, since
Our class will declare that the next step is to instantiate the object.
When the class is defined, we use the New keyword to generate an object.
Code fragment
Copy Code code as follows:

$ Object name = new class name ();
<?php
Class person{
The following are the member properties of the person
var $name; Person's name
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 man can talk.
echo "This man is talking";
function Run () {
The way this man 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 the object name of our instance, and $p 2,
$p 3 is also the object name of our example, a class can instantiate multiple objects, each object is independent, on
Face code equivalent to an instance of 3 people come, there is no connection between each other, can only show that they are human, every
Individuals have their own names, gender and age attributes, and everyone has a way of talking and walking, as long as it's inside the class.
The member properties and member methods that come out are included in the instantiated object.
Like in PHP and integral type, floating-point type, is 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 is how to embody it? The memory logically
Said in general is divided into 4 paragraphs, stack space segment, heap space segment, code section, initialization static section, the program inside different statements
In different memory segments, the stack space segment is the place to store data types that occupy the same length of space and occupy a small amount of space.
side, such as integral type 1,10,100,1000,10000,100000, etc., occupy the space in the memory is long,
are 64-bit 4 bytes. So the data length is indefinite, and the data type that occupies a large space is placed in that memory
In that paragraph? Such data is placed in the heap memory. Stack memory can be accessed directly, while heap memory is
Memory that cannot be accessed directly. The number of our objects is a large data type and takes up an indefinite amount of space.
Type, so the object is placed in the heap, but the object name is placed inside the stack, so that the object name can be
To use the object.
$p 1=new person ();
For this piece of code, $p 1 is the object name in the stack memory, and the new person () is the real object in the heap memory
Inside, specific please look at the following figure:
' This.width= '; if (this.height> ') ' this.height= '; border=0>  
The $p1=new person () can be seen from the figure above, and the right side of the equal sign is the real object instance, the entity inside the heap memory,
There are 3 new person () in the diagram above, so there are 3 spaces in the heap that produce 3 instance objects, each
Are independent of each other, using their own space, in PHP, as long as there is a new keyword appears will
Instantiate an object and open up a space within the heap.
Each instance object in the heap is a storage attribute, for example, now the instance object inside the heap contains the last name
Name, sex and age. Each attribute also has an address.
$p 1=new person (); The right side of the equal sign is a reference variable, and the first address of the object is $P1 by the assignment operator "="
To the "$p 1" reference variable, so $P1 is the variable that stores the first address of the object, $p 1 is placed inside the stack memory, $p 1 is quite
To a pointer to an object inside the heap, so we can manipulate the object by $P1 the reference variable, and we usually
The object reference is called an object.
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
To 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 an object member:
Object-> Property $p1->name; $p 2->age; $p 3->sex;
Object-> method $p1->say (); $p 2->run ();
As in the following example:
Code fragment
Copy Code code as follows:

<?php
Class person{
The following are the member properties of the person
var $name; Person's name
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
$p 2->say ();
$p 2->run ();
The next three lines are assigning values to the $p3 object property
$p 3->name= "Harry";
$p 3->sex= "Male";
$p 3->age=40;
The following three lines are the properties that access the $p3 object
The name of the echo "P3 object is:". $p 3->name. " <br> ";
The gender of the Echo "P3 object" is: ". $p 3->sex." <br> ";
The age of the echo "P3 object" is: ". $p 3->age." <br> ";
The following two lines access methods in the $p3 object
$p 3->say ();
$p 3->run ();
?>

As can be seen from the example above, only the members of the object are accessed using the Object-> property, Object-> method, and
There is no second way to access a member of an object.

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.