5. How to instantiate objects
We said above that the unit of an object-oriented program is an object, but objects are instantiated through classes. Since
our class will be declared, let’s follow The first step is to instantiate the object.
After defining the class, we use the new keyword to generate an object.
Code snippet
Copy code The code is as follows:
$object name = new class name();
class Person{
//The following are the member attributes of the person
var $name; //The name of the person
var $sex; //The gender of the person
var $ age; //Age of a person
//Here are the member methods of a person
function say(){
//Method that this person can talk
echo "This person is talking";
} function run(){
//How this person can walk
echo "This person is walking";
}
}
$p1=new Person();
$p2=new Person();
$p3=new Person();
?>
$p1=new Person();
This code is In the process of generating instance objects through a class, $p1 is the name of the object we instantiate. Similarly, $p2 and
$p3 are also the names of the objects we instantiate. A class can instantiate multiple objects, and each object It is independent. The code above
is equivalent to the example of 3 people. There is no connection between each person. It can only mean that they are all human beings. Each
person has his own name, gender and age. Attributes, everyone has a way to talk and walk. As long as they are member attributes and member methods reflected in the class, the instantiated object will contain these attributes and methods.
Objects in PHP, like integers and floating point types, are also a data class, which are used to store different types of data.
They must be loaded into memory during runtime, so objects How is it reflected in the memory? Memory is logically
divided into 4 segments, stack space segment, heap space segment, code segment, and initialization static segment. Different declarations in the program
are placed in different memory segments. The stack space segment It is a place to store data types that occupy the same space length and take up small space
, such as integers 1, 10, 100, 1000, 10000, 100000, etc. The space occupied in the memory is the same length,
Both are 64 bits and 4 bytes. So what about the data type whose data length is not fixed and occupies a large space and is placed in the segment of memory
? Such data is placed in the heap memory. Stack memory can be directly accessed, while heap memory is
memory that cannot be directly accessed. For our object, it is a large data type and it takes up a variable length
, so the object is placed in the heap, but the object name is placed in the stack, so the object name is The object is
ready for use.
$p1=new Person();
For this code, $p1 is the object name in the stack memory, and new Person() is the real object in the heap memory
. For details, please Look at the picture below:
=700) window.open('/upload/20090930215213451.gif');" src="http://www.bkjia.com/uploads/allimg/131016/09212313K-0 .gif" onload="if(this.width>'700')this.width='700';if(this.height>'700')this.height='700';" border=0>
Assign to the reference variable "$p1", so $p1 is the variable that stores the first address of the object. $p1 is placed in the stack memory. $p1 is equivalent to a pointer pointing to the object in the heap, so we can pass $p1 is a reference variable used to operate objects. Usually we also
call object references
6. How to use members in objects
As seen above, there are two types of members in PHP objects. Member attributes, one is member method. We can declare the object with
. How to use the members of the object? To access the members of the object, we need to use a
Special operator "->" to complete access to object members:
Object->Properties $p1->name; $p2->age; $p3->sex;
Object ->Method $p1->say(); $p2->run();
As the following example:
Code snippet
Copy code
The code is as follows:
class Person{
//The following are the member attributes of the person
var $name; //The person’s name
var $sex; //The person’s Gender
var $age; //The age of the person
//The following is the member method of the person
function say(){ //The method by which this person can speak
echo "This person is talking" ;
}
function run(){ //How this person can walk
echo "This person is walking";
}
}
$p1=new Person() ; //Create instance object $p1
$p2=new Person(); //Create instance object $p2
$p3=new Person(); //Create instance object $p3
//Below The three lines are to assign values to the $p1 object attributes
$p1->name="Zhang San";
$p1->sex="male";
$p1->age=20;
//The following three lines are to access the properties of the $p1 object
echo "The name of the p1 object is: ".$p1->name."
";
echo "The name of the p1 object The gender is: ".$p1->sex."
";
echo "The age of the p1 object is: ".$p1->age."
";
/ /The following two lines access the methods in the $p1 object
$p1->say();
$p1->run();
//The following three lines assign values to the properties of the $p2 object
$p2->name="李思";
$p2->sex="Female";
$p2->age=30;
//The following three lines are Access the properties of the $p2 object
echo "The name of the p2 object is:".$p2->name."
"
echo "The gender of the p2 object is:".$p2-> sex."
";
echo "The age of the p2 object is: ".$p2->age."
";
//The following two lines access the $p2 object Method
$p2->say();
$p2->run();
//The following three lines assign values to the properties of the $p3 object
$p3->name= "王五";
$p3->sex="male";
$p3->age=40;
//The following three lines are to access the properties of the $p3 object
echo "The name of the p3 object is: ".$p3->name."
";
echo "The gender of the p3 object is: ".$p3->sex."
";
echo "The age of the p3 object is: ".$p3->age."
";
//The following two lines access the methods in the $p3 object
$p3-> say();
$p3->run();
?>
It can be seen from the above example that only the members in the object need to use the object-> Attributes and objects are accessed in the form of methods.
There is no second way to access members in the object.
http://www.bkjia.com/PHPjc/320656.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320656.htmlTechArticle5. How to instantiate an object. We said above that the unit of an object-oriented program is an object, but an object is passed through a class. Instantiated, now that our class has been declared, the next step is the instance...