How do I instantiate an object by getting started with the PHP object-oriented (OOP) programming tutorial ————?

Source: Internet
Author: User

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 ();

<?PHPclassperson{//The following is the person's member property    var $name;//Man's name    var $sex;//Man's Sex    var $age;//person's age//Below is the method of the person's membership    functionSay () {//The way this man can talk.        Echo"This man is talking."; }     functionRun () {//The way this man can walk.        Echo"This man is walking."; }} $p 1=NewPerson ();$p 2=NewPerson ();$p 3=NewPerson ();?>

$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 broadly divided into 4 segments, stack space , heap space , code Snippets , the initial static section ,

①. Stack space segment

The feature of the stack is that the space is small but is accessed by the CPU fast, and is a temporary variable created in the user's storage program. Because of the last-in-first-out feature of the stack, the stack is particularly handy for saving and recovering call sites. In this sense, we can think of the stack as a temporary data storage, swap memory area. Memory segments that are used to store data types that occupy a constant amount of space and occupy little space, such as Integer 1, 100, 10000, and so on, are equal in memory, and occupy space is 4 bytes of 32 bits. There are also double, Boolean, and so on can be stored in the stack space segment.

②. Heap Space Segments

A heap is a memory segment that is allocated dynamically during a process run and is not fixed in size and can be dynamically expanded or scaled down. Used to store data with variable length or large memory consumption. For example, strings, arrays, and objects are stored in this memory.

③. Data segment

Data segments are used to hold the initialization of global variables in an executable file, in other words, variables that are statically allocated by the program.

④. Code Snippets

A code snippet is an action instruction used to hold an executable file, which means that it is a mirror image of a runnable program in memory. Code snippets need to be protected from illegal modification at run time, so only read operations are allowed, not write (modify) operations. For example, a function in a program is stored in this memory.

Object type data is a large space-occupying data type, and is a variable space-consuming data type, so object creation is stored in memory, but the object's reference is stored in the stack. While the program is running, the data in memory is directly accessible, and heap memory is memory that cannot be accessed directly, but members of the object can be accessed through the object's reference name.

The different statements inside the program are placed in different memory segments,

The stack space segment is where the data types that occupy the same length of space and occupy small space, such as Integer 1, 10, 100, 1000, 10000, 100000, etc., Occupy space in memory is equal to, all 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.

Verify:

classperson{ Public $name;}$obj 1=NewPerson ();$obj 1->name = "Test1";Echo $obj 1-name;$obj 2=$obj 1;$obj 2->name = "Test2";Echo $obj 1-name;Echo $obj 2->name;

Judging by the test results, the explanation is right.
$p 1 is a pointer to an object and not the object itself , Obj2 and the obj1 all point to the same piece of memory , Same Object . This is the same as the OOP language

Object (person) [2]
Public ' name ' = = String ' test2 ' (length=5)

Object (person) [2]
Public ' name ' = = String ' test2 ' (length=5)
The ID number of the visible object is a

If you want a copy of an object, use $obj2 =clone $obj 1; Using clone will create a new object, allocating memory, independent of the original obj1
See Manual for this page http://www.php.net/manual/zh/language.oop5.cloning.php

$obj 2 = $obj 1;
$obj 2 = & $obj 1;
The same effect, the same explanation?
For object, it is the same. It is not the same for normal variables.
$a = 1;
$b = $a;
$c = & $a;
Not the same.

PHP Object-oriented (OOP) programming Getting Started Tutorial ———— How do I instantiate an object?

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.