For example, an employee management application might include an EmPloyee class. This class can then be used to create and maintain specific instances, such as Gonn and Sally.
Creating objects based on predefined classes is often called class instantiation.
The object is created using the new keyword, as follows:
. The code is as follows:
$employee = new Employee();
After creating the object, the newly instantiated object has the attributes defined in the class All properties and actions.
How to instantiate objects
The unit of object-oriented programming is the object, but the object is instantiated through the class. Now that our class has been declared, the next step is to instantiate the object. After defining the class, we use the new keyword to generate an object.
. The code is as follows:
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; // 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() //This person can How to 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 name of the object we instantiate. Similarly, $p2 and $p3 are also the names of the objects we instantiate. A class Multiple objects can be instantiated, and each object is independent. The above code is equivalent to instantiating 3 people. There is no connection between each person. It can only mean that they are all human beings, and each person has his or her own name. , attributes of gender and age, everyone has a way of talking and walking, 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. They are used to store different types of data. They must be loaded into memory for use during runtime. Then the objects in the memory are How is it reflected? Logically speaking, memory is roughly 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 occupies the same space for storage. Data types that are small in length and occupy space, such as integers 1, 10, 100, 1000, 10000, 100000, etc., occupy the same length of space in the memory, and are all 64 bits and 4 bytes. So where should the data of a data type that has a variable length and takes up a lot of space be placed in that memory segment? Such data is placed in the heap memory. Stack memory can be directly accessed, while heap memory cannot be directly accessed. For our object, it is a large data type and it takes up a variable length of space. Therefore, the object is placed in the heap, but the object name is placed in the stack, so that 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, the entity in the heap memory. There are 3 times of new Person() here, so 3 spaces will be opened up in the heap and 3 instance objects will be generated. Each object is independent of each other and uses its own space. In PHP, as long as there is a new When this keyword appears, an object will be instantiated and a space of its own will be created in the heap.
Each instance object in the heap stores attributes. For example, the instance objects in the heap now all store name, gender and age. Each attribute in turn has an address. $p1=new Person(); The right side of the equal sign $p1 is a reference variable. The first address of the object is assigned to the reference variable "$p1" through the assignment operator "=", so $p1 stores the first address of the object. Variable, $p1 is placed in the stack memory. $p1 is equivalent to a pointer pointing to an object in the heap, so we can operate the object through the reference variable $p1. Usually we also call the object reference an object.
For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!