PHP Object-oriented (OOP) programming Full Tutorial: 5. How do I instantiate an object?

Source: Internet
Author: User
Tags php tutorial
As we said above, object-oriented programs are objects, but objects are instantiated through classes, and since our class declares, the next step is to instantiate the object.

When a class is defined, we use the New keyword to generate an object.

$ Object name = new class name ();


 
  


$p 1=new person ();

This code is the process of generating an instance object through a class, $p 1 is the name of the object we are using as an example, the same, $p 2, $p 3 is also the object name of our example, a class can instantiate multiple objects, each object is independent, the above code is equivalent to the instance of 3 people, There is no connection between each person, can only show that they are human, everyone has their own name, gender and age attributes, everyone has a way to talk and walk, as long as the class is reflected in the member properties and member methods, the instantiated object contains these properties and methods.

Like in PHP and integer, floating point type, is also a kind of data class, are stored in different types of data, in the run time to load into memory to use, then the object in memory is how to reflect it? Memory from the ROM is generally divided into 4 paragraphs, stack space, heap space, code snippets, the initial static section, the program inside the different statements placed in different memory segments, the stack space is the same space to occupy the same length and occupy a small space of data types, such as Integer 1, 10, 100, 1000, 10000, 100000, etc., in the memory space is equal to the length, are 64 bits 4 bytes. So the data length is variable, and the data type with large space is placed in that segment of memory? This data is placed in the heap memory. Stack memory is directly accessible, and heap memory is memory that cannot be accessed directly. For our object is a kind of large data type and take up a variable length of the type, so that the object is placed in the heap, but the object name is placed in the stack, so that the object name can be used by the object.

$p 1=new person ();

For this code, $p 1 is the object name in the stack memory, the new person () is the real object is inside the heap memory, the concrete see:


It can be seen from the $p1=new person (); The right side of the equals sign is a real object instance, in the heap memory of the entity, a total of 3 times the new person (), so will be in the heap open 3 space, produce 3 instance objects, each object is independent of each other, using their own space, In PHP, as soon as a new keyword appears, an object is instantiated, creating a space within the heap.

Each instance object in the heap stores properties, for example, the instance object inside the heap now contains names, genders, and ages. Each property also has an address.

$p 1=new person (); The left $p1 of the equals sign is a reference variable that assigns the first address of the object to the reference variable "$p 1" through the assignment operator "=", so $P1 is the variable that stores the first address of the object, $p 1 is placed in the stack memory, $p 1 is equivalent to a pointer pointing to the object inside the heap, So we can manipulate the object by $P1 this reference variable, and we usually refer to it as an object.


The above describes the PHP object-oriented (OOP) programming Full Tutorial: 5. How do I instantiate an object?, including the content of the aspects, I want to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.