- In PHP: the difference between this, self and parent
- this: Pointer to the current object
- self: refers to the directed class itself
- Parent: A pointer to the parent class
- summary
- Hot Keywords
- Are there pointers in php?
- Php Solutions
- Solution 1 — Php
- Solution 2 — Php
- There are number of things that acts similar to pointers
- Solution 3 — Php
- Solution 4 — Php
- Solution 5 — Php
- Solution 6 — Php
- Solution 7 — Php
- Solution 8 — Php
- Solution 9 — Php
In PHP: the difference between this, self and parent
Here I mainly talk about the difference between the three keywords ** this,self,parent **. Literally, it refers to this, myself and father respectively. To begin with, this is a pointer to the current object (which can be seen as a pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class.
this: Pointer to the current object
name =$name; //this pointer statement has been used here. > //Destructor function __destruct()<> //Print User Name Membership Functions function printname() < print( $this->name); //Use this pointer statement again, or echo output > > $obj1 = new name("PBPHome"); //Instantiate Object Statement (3) //Execution printing $obj1->printname(); //Output: PBPHome echo"
"; //Output: Return //Second Instance Object $obj2 = new name( "PHP" ); //Execution printing $obj2->printname(); //Output: PHP ?>
Explanation: The above classes use this pointer in statement 1 and statement 2 respectively, so who is this pointer to at that time?
Actually ** this is when you instantiate to determine who ** is pointing to, such as when you first instantiate an object (statement 3).
Then this was pointing to the $obj1 object.
Then print ($this — > < name) becomes print ($obj1t - >name) when executing statement 2.
Then, of course, «PBPHome» is exported.
In the second instance, print ($this — > name) becomes print ($obj2 — > name), and «PHP» is output.
So this is a pointer to the current object instance, not to any other object or class.
self: refers to the directed class itself
First of all, let’s make it clear that self refers to the class itself, that is, self does not refer to any object that has been instantiated. Generally, self is used to refer to static variables in the class.
If we use static members of a class, we must also use self to call.
Also note that using self to invoke static variables must use **:** (field operation symbols), as shown in the example.
lastCount =++self::$firstCount; //Use self to call static variable statements > //Print lastCount values function printLastCount() < print( $this->lastCount ); > > //Instance object $obj = new Counter(); $obj->printLastCount(); //When it is executed here, the program outputs 1 ?>
Here we should pay attention to two local sentences (1) and (2) sentences.
We define a static variable $firstCount in statement 1, then use self to call this value in statement 2, then we call the static variable $frestCount defined by the class itself.
Our static variables have nothing to do with the instance of the following object. They are only related to the class. If I call the class itself, then we can’t use this to refer to it, because self refers to the class itself and has nothing to do with any object instance.
And then the first thing called with this is the instantiated object, $obj, so don’t get confused.
Parent: A pointer to the parent class
First, we make it clear that parent is a pointer to the parent class. Generally, we ** use parent to call the constructor of the parent class. Examples are as follows:
name = $name; > > //Define the derived class Person to inherit from the Animal class class Person extends Animal < public$personSex; //For derived classes, the attributes $personSex gender, $personAge age are newly defined public $personAge; //Constructors of Derived Classes function __construct( $personSex, $personAge ) < parent::__construct( "PBPHome"); //Use parent to call the constructor statement of the parent class $this->personSex = $personSex; $this->personAge = $personAge; > //Derived class member function for printing, format: name is name,age is age function printPerson() < print( $this->name. " is ".$this->personSex. ",age is ".$this->personAge ); > > //Instantiate Person objects $personObject = new Person( "male", "21"); //Execution printing $personObject->printPerson();//Output: PBPHome is male,age is 21 ?>
It also contains the use of this, we analyze it ourselves.
Let’s take note of the details: member attributes are public (public attributes and methods, accessible to both internal and external code of the class), especially the parent class, for inheritance classes to access through this.
The key point is statement 1: parent:: construct («heiyeluren»), at which time we use parent to call the constructor of the parent class to initialize the parent class, so that the inherited class objects are assigned a name of PBPHome.
We can test that by instantiating an object, $personObject1, the name is still PBPHome after printing.
summary
this is a pointer to an object instance, which is determined when it is instantiated.
self is a reference to the class itself and is generally used to point to static variables in the class.
Parent is a reference to the parent class. Parent is usually used to call the constructor of the parent class.
Posted by cnnabc on Tue, 07 May 2019 14:15:39 -0700
Hot Keywords
- Java — 6961
- Database — 2683
- Python — 2616
- Programming — 2419
- Attribute — 2418
- Javascript — 2383
- Spring — 2111
- Android — 2077
- xml — 1972
- Linux — 1818
- JSON — 1764
- network — 1748
- github — 1720
- less — 1676
- MySQL — 1538
- SQL — 1332
- PHP — 1318
- encoding — 1179
- Mobile — 1029
- Apache — 925
Are there pointers in php?
What does this code mean? Is this how you declare a pointer in php?
Php Solutions
Solution 1 — Php
Variable names in PHP start with $ so $entryId is the name of a variable. $this is a special variable in Object Oriented programming in PHP, which is reference to current object. -> is used to access an object member (like properties or methods) in PHP, like the syntax in C++. so your code means this:
Place the value of variable $entryId into the entryId field (or property) of this object.
The & operator in PHP, means pass reference. Here is a example:
$b=2; $a=$b; $a=3; print $a; print $b; // output is 32 $b=2; $a=&$b; // note the & operator $a=3; print $a; print $b; // output is 33
In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.
In PHP, arguments are passed by value by default (inspired by C). So when calling a function, when you pass in your values, they are copied by value not by reference. This is the default IN MOST SITUATIONS. However there is a way to have pass by reference behaviour, when defining a function. Example:
function plus_by_reference( &$param ) < // what ever you do, will affect the actual parameter outside the function $param++; > $a=2; plus_by_reference( $a ); echo $a; // output is 3
There are many built-in functions that behave like this. Like the sort() function that sorts an array will affect directly on the array and will not return another sorted array.
There is something interesting to note though. Because pass-by-value mode could result in more memory usage, and PHP is an interpreted language (so programs written in PHP are not as fast as compiled programs), to make the code run faster and minimize memory usage, there are some tweaks in the PHP interpreter. One is lazy-copy (I’m not sure about the name). Which means this:
When you are coping a variable into another, PHP will copy a reference to the first variable into the second variable. So your new variable, is actually a reference to the first one until now. The value is not copied yet. But if you try to change any of these variables, PHP will make a copy of the value, and then changes the variable. This way you will have the opportunity to save memory and time, IF YOU DO NOT CHANGE THE VALUE.
$b=3; $a=$b; // $a points to $b, equals to $a=&$b $b=4; // now PHP will copy 3 into $a, and places 4 into $b
After all this, if you want to place the value of $entryId into ‘entryId’ property of your object, the above code will do this, and will not copy the value of entryId, until you change any of them, results in less memory usage. If you actually want them both to point to the same value, then use this:
Solution 2 — Php
No, As others said, «There is no Pointer in PHP.» and I add, there is nothing RAM_related in PHP.
And also all answers are clear. But there were points being left out that I could not resist!
There are number of things that acts similar to pointers
- eval construct (my favorite and also dangerous)
- $GLOBALS variable
- Extra ‘$’ sign Before Variables (Like prathk mentioned)
- References
First one
At first I have to say that PHP is really powerful language, knowing there is a construct named «eval», so you can create your PHP code while running it! (really cool!)
although there is the danger of PHP_Injection which is far more destructive that SQL_Injection. Beware!
So instead of using a pointer to act like another Variable, You Can Make A Variable From Scratch!
Second one
$GLOBAL variable is pretty useful, You can access all variables by using its keys.
$three="Hello";$variable=" Amazing ";$names="World"; $arr = Array("three","variable","names"); foreach($arr as $VariableName) echo $GLOBALS[$VariableName];
Note: Other superglobals can do the same trick in smaller scales.
Third one
You can add as much as ‘$’s you want before a variable, If you know what you’re doing.
$a="b"; $b="c"; $c="d"; $d="e"; $e="f"; echo $a."-"; echo $$a."-"; //Same as $b echo $$$a."-"; //Same as $$b or $c echo $$$$a."-"; //Same as $$$b or $$c or $d echo $$$$$a; //Same as $$$$b or $$$c or $$d or $e
Last one
Reference are so close to pointers, but you may want to check this link for more clarification.
function junk(&$tion) $GLOBALS['a'] = &$tion;> $a="-Hello World
"; $b="-To You As Well"; echo $a; junk($b); echo $a;
> -Hello World > > -To You As Well
Solution 3 — Php
That syntax is a way of accessing a class member. PHP does not have pointers, but it does have references.
The syntax that you’re quoting is basically the same as accessing a member from a pointer to a class in C++ (whereas dot notation is used when it isn’t a pointer.)
Solution 4 — Php
To answer the second part of your question — there are no pointers in PHP.
When working with objects, you generally pass by reference rather than by value — so in some ways this operates like a pointer, but is generally completely transparent.
This does depend on the version of PHP you are using.
Solution 5 — Php
You can simulate pointers to instantiated objects to some degree:
class pointer < var $child; function pointer(&$child) < $this->child = $child; > public function __call($name, $arguments) < return call_user_func_array( array($this->child, $name), $arguments); > >
$a = new ClassA(); $p = new pointer($a);
If you pass $p around, it will behave like a C++ pointer regarding method calls (you can’t touch object variables directly, but that’s evil anyways 🙂 ).
Solution 6 — Php
entryId is an instance property of the current class ($this) And $entryId is a local variable
Solution 7 — Php
Yes there is something similar to pointers in PHP but may not match with what exactly happens in c or c++. Following is one of the example.
$a = "test"; $b = "a"; echo $a; echo $b; echo $$b; //output test a test
This illustrates similar concept of pointers in PHP.
Solution 8 — Php
PHP passes Arrays and Objects by reference (pointers). If you want to pass a normal variable Ex. $var = ‘boo’; then use $boo = &$var;.
Solution 9 — Php
PHP can use something like pointers:
Now $y acts like a pointer to $x and $y[0] dereferences a pointer.
The value array(&$x) is just a value, so it can be passed to functions, stored in other arrays, copied to other variables, etc. You can even create a pointer to this pointer variable. (Serializing it will break the pointer, however.)