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

Source: Internet
Author: User
5. How to instantiate an object
As we said above, object-oriented programs are objects, but objects are instantiated by class, since
Our class will declare that the next step is to instantiate the object.
When a class is defined, we use the New keyword to generate an object.
Code Snippets
Copy CodeThe code is as follows:
$ Object name = new class name ();
Class person{
The following is the person's member property
var $name; Man's name
var $sex; Man's Sex
var $age; The age of the person
The following is a member method 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 through a class, $p 1 is the name of the object we are using as an instance, $p 2,
$p 3 is also the name of the object that we instantiate, a class can instantiate multiple objects, each object is independent,
The code of the polygon is equivalent to an instance out of 3 people, who are not connected to each other, can only show that they are human beings, every
Individuals have their own names, gender and age attributes, and everyone has the means to speak and walk, as long as the body is inside the class
The existing member properties and member methods, which are included in the instantiated object, contain these properties and methods.
As in PHP and integral type, floating point type, is also a kind of data class, are stored in different types of data,
In the run time to load into the memory to use, then the object in memory is how to reflect it? The memory logically
Said in general is divided into 4 paragraphs, stack space, heap space, code snippets, initialization of static section, the application of different statements
In different memory segments, the stack space is the place to store the data types that occupy the same space length and occupy little space
such as integral type 1,10,100,1000,10000,100000 and so on, in memory occupy space is equal length,
are 64-bit, 4-byte. So the data length is variable, and the data types that occupy a large space are placed in that memory
In that section? This data is placed in the heap memory. Stack memory is directly accessible, and heap memory is
Memory that cannot be accessed directly. The number of our objects is a large data type and takes up a long space
, 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 code, $p 1 is the object name in the stack memory, and the new person () is the real object that is in the heap memory
Inside, see for specific:
=700) window.open ('/upload/20090930215213451.gif '); "src=" http://www.bkjia.com/uploads/allimg/131016/ 09212313k-0.gif "onload=" if (this.width> ') this.width= ' "," if (this.height> ' ") this.height= ' 700 ';" Border=0>
You can see $p1=new person (); The right side of the equals sign is the real object instance, the entity inside the heap memory,
A total of 3 new person (), so there will be 3 space in the heap, resulting in 3 instance objects, each object
Are independent of each other, using their own space, in PHP, as long as there is a new this keyword appears will
Instantiate an object that opens up a space in the heap.
Each instance object inside the heap is stored as a property, for example, the instance object inside the heap now contains a surname
Name, gender and age. Each property also has an address.
$p 1=new person (); The right side of the equal sign $P1 is a reference variable that assigns the first address of the object by the assignment operator "="
This reference variable is assigned to "$p 1", so $P1 is the variable that stores the first address of the object, $p 1 is placed in the stack memory, $p 1 is quite
To a pointer to the object inside the heap, so we can manipulate the object by $P1 this reference variable, and usually we
Called object references as objects.
6. How to use members in an object
There are two types of members in a PHP object that are member properties, and one is a member method. We have been able to
To declare, $p 1=new person (); How to use the members of the object? To access members of an object, use a
Special operator, "-" to complete access to Object members:
Object, Property $p1->name; $p 2->age; $p 3->sex;
Object---method $p1->say (); $p 2->run ();
As in the following example:
Code Snippets
Copy CodeThe code is as follows:
Class person{
The following is the person's member property
var $name; Man's name
var $sex; Man's Sex
var $age; The age of the person
The following is a member method 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 an Instance object $p1
$p 2=new person (); Create an Instance object $p2
$p 3=new person (); Create an Instance object $p3
The following three lines are assigned a value to the $p1 object property
$p 1->name= "Zhang San";
$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. "
”;
The gender of the echo "P1 object is:". $p 1->sex. "
”;
The age of the echo "P1 object is:". $p 1->age. "
”;
The following two lines access the methods in the $p1 object
$p 1->say ();
$p 1->run ();
The following three lines are assigned a value to the $p2 object property
$p 2->name= "John Doe";
$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. "

The gender of the echo "P2 object is:". $p 2->sex. "
”;
The age of the echo "P2 object is:". $p 2->age. "
”;
The following two lines access the methods in the $p2 object
$p 2->say ();
$p 2->run ();
The following three lines are assigned a value 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. "
”;
The gender of the echo "P3 object is:". $p 3->sex. "
”;
The age of the echo "P3 object is:". $p 3->age. "
”;
The following two lines access the methods in the $p3 object
$p 3->say ();
$p 3->run ();
?>

As you can see from the above example, only the members of the object are accessed using the object----Properties, Object-by-method, and
There is no second way to access the members of the object.

http://www.bkjia.com/PHPjc/320656.html www.bkjia.com true http://www.bkjia.com/PHPjc/320656.html techarticle 5. How to instantiate an object we said that the object-oriented program is the object, but the object is also instantiated through the class, since our class will declare, the next step is the instance ...

  • Related Article

    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.