Class declaration here !! 5. how to instantiate an object we mentioned above that the unit of the object-oriented program is an object, but the object is instantiated through the class. since the class 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 (); "> <LINKhref =" http: // www. php1 class declaration, let's get here !!
5. how to instantiate an object 6. how to use members in an objectAs we have mentioned above, the unit of the object-oriented program is the object, but the object is instantiated through the class. since our class 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 the class. $ p1 is the object name of our instance. Similarly, $ p2, $ p3 is the name of the object from our instance. a class can be used to generate multiple objects. each object is independent. the code above is equivalent to three people from the instance, there is no connection between everyone. it only means that they are all human beings. everyone has their own name, gender, and age. everyone has a way to speak and walk, as long as it is the member attributes and member methods embodied in the class, the instantiated object contains these attributes and methods. Like in PHP, it is also a data type that stores different types of data, and must be loaded to the memory during running, how is the object reflected in the memory? Generally speaking, memory is divided into four segments: stack space segments, heap space segments, code segments, and initialization static segments. different declarations in the program are placed in different memory segments, stack space segments are data types that occupy the same length of storage space and small space, such as integer 1, 10,100,100 0, 10000,100 000, and so on, all are 64-bit and 4-byte. So the data length is not long, and the data type that occupies a large space is put in that memory segment? Such data is stored in the heap memory. Stack memory can be directly accessed, while Stack memory cannot be directly accessed. For our object count, it is a big data type that occupies an indefinite amount of space. Therefore, the object is placed in the heap, but the object name is placed in the stack, in this way, the object can be used through the object name. $ 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, see: we can see that $ p1 = new Person (); the right side of the equal sign is a real object instance. There are three new persons () in the heap memory (), therefore, three instances are created in the heap to create three instance objects. each object is independent of each other and uses its own space. in PHP, as long as there is a new keyword, an object will be instantiated and a space will be opened up in the heap. Each instance object in the heap is stored. for example, the instance object in the heap contains the name, gender, and 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 to the reference variable "$ p1" through the value assignment operator "=, therefore, $ p1 is the variable that stores the first address of an object. $ p1 is placed in the stack memory. $ p1 is equivalent to a pointer pointing to an object in the heap, therefore, we can operate on objects through the reference variable $ p1, which is also called Object reference. The above shows that the PHP object has two types of members: Member attributes and member methods. The object can be declared. $ p1 = new Person (); how can we use the object members? To access members of an object, you must use a special operator "->" to access the 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 the $ p1 object. echo "the name of the p1 object is :". $ 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 are the attributes of $ p2 object. echo "p2 object name is :". $ 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 are the attributes of the $ p3 object. echo "the name of the p3 object is :". $ p3-> name."
"; Echo" the sex of the p3 object is: ". $ p3-> sex ."
"; Echo" the age of the p3 object is: ". $ 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 need to access the members in the object in the form of object-> attribute, object-> method, and there is no second method to access the members in the object.