Home  >  Article  >  Backend Development  >  Introduction to the concept of PHP object _PHP tutorial

Introduction to the concept of PHP object _PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:18841browse

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:

Copy code The code is as follows:
$employee = new Employee();


After the object is created, the newly instantiated object has all the properties and behaviors defined in the class.

How to instantiate an object
The unit of an object-oriented program is an object, but an object is instantiated by a 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.
Copy code The code is as follows:

class Person
{
//The following are the member attributes of person
var $name; //The person’s name
var $sex; //The person’s gender
var $age; //The person’s age

//The following are the person’s member methods
function say() //How this person can talk
{
echo "This person is talking";
}

function run() //This person can walk Method
{
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 our instance In the same way, $p2 and $p3 are also the names of the objects we instantiate. A class can instantiate multiple objects, and each object is independent. The above code is equivalent to 3 people instantiating each instance. There is no connection between individuals. It can only mean that they are all human beings. Everyone has their own name, gender and age attributes. Everyone has a way of talking and walking. As long as the member attributes and attributes reflected in the class are Member methods, the instantiated object contains these properties 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 during operation. Then objects How is it reflected in the memory? 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 with variable length and large space occupancy 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 a total of 3 times of new Person() here, so 3 spaces will be opened 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325553.htmlTechArticleFor 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn