Class provides a basis for creating a specific instance of an object (that is, the object modeled by this class) on this basis. these specific instances are called objects. for example, the EmPloyee management application may include an EmPloyee class. Then you can use this class to create and maintain specific instances, such as Gonn and Sally.
Objects created based on predefined classes are often called class instantiation ).
The object is created with the new keyword, as shown below:
The code is as follows:
$ Employee = new Employee ();
After an object is created, the instantiated object has all the properties and behaviors defined in the class.
How to instantiate an object
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.
The code is as follows:
Class Person
{
// The following are the member attributes of a person.
Var $ name; // The name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// The following is the member method of a person.
Function say () // The way this person can speak
{
Echo "this person is talking ";
}
Function run () // this person can walk
{
Echo "this person is walking ";
}
}
$ P1 = new Person ();
$ P2 = new Person ();
$ P3 = new Person ();
$ P1 = new Person ();
This code is the process of generating instance objects through classes. $ p1 is the object name of our instance. Similarly, $ p2 and $ p3 are also the object names of our instances, A class can instance multiple objects, each of which is independent. the code above is equivalent to three people coming out of the instance. there is no connection between each person. it only means that they are all human beings, each person has his/her own name, gender, and age. each person 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.
The right side of the equal sign is the real object instance, which is 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 object is independent of each other and uses its own space, in PHP, an object will be instantiated as long as there is a new keyword, opening up a space 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.