When to use $ this-> property instead of $ property in PHP

Super easy question. Look at the 2 classes of class methods.

In the first I pass the $params variable / property call Then I do $this->params

My question is whether this is really necessary, I usually do it this way, but I noticed that it will work in the second example with just calling $params without setting $this .

So my theory is this ... You should set it as $this->params if you need to access this property with another method in this class, and you can only use $params if you use this property only in the same the method is already there.

Can someone shed some light on this and explain whether my theory is correct, or if I am leaving, I would like to know the reasons for this, so I will know when I do each method or do one or the other all the time, thank you

 class TestClass{ public function TestFunc($params){ $this->params = $params; echo 'testing this something'. $this->params; } } 

without defining variables

 class TestClass2{ public function TestFunc2($params){ echo 'testing this something'. $params; } } 
+4
source share
5 answers

Use $this when accessing class variables.

When referring to a variable, which is actually a parameter in a function, there is no need to use the $this . In fact, to access a function parameter with $ params parameters, you should not use $ this keyword ...

In your example:

 class TestClass{ public function TestFunc($params){ $this->params = $params; echo 'testing this something'. $this->params; } } 

$params from TestFunc($params){ is a parameter / argument of the TestFunc function, so you do not need to use $this . In fact, to access the parameter value, you should not use $this - now that you have used $this->params from $this->params = $params = $params; , you actually set the value equivalent to the value of the $params parameter to a NEW class- level variable with the name also $params (since you did not declare it anywhere in your code example)

[edit] based on the comment:

Take a look at this example:

 class TestClass{ public function TestFunc($params){ $this->params = $params; # ^ you are setting a new class-level variable $params # with the value passed to the function TestFunc # also named $params echo 'testing this something'. $this->params; } public function EchoParameterFromFunction_TestFunc() { echo "\n\$this->params: " . $this->params . "\n"; # now you are echo-ing the class-level variable named $params # from which its value was taken from the parameter passed # to function TestFunc } } $tc = new TestClass(); $tc->EchoParameterFromFunction_TestFunc(); # error: undefined property TestClass::$params $tc->TestFunc('TestFuncParam'); $tc->EchoParameterFromFunction_TestFunc(); # should echo: $this->params: TestFuncParam 

The error when calling EchoParameterFromFunction_TestFunc without first calling TestFunc is the result of not declaring / setting a class variable / property with the name $params - you specified this inside TestFunc , which means that it is not set unless you call TestFunc . To install it correctly so that everyone can immediately access it:

 class TestClass{ # declare (and set if you like) public /*or private or protected*/ $params; // = ''; or create a construct... public function __construct(){ # set (and first declare if you like) $this->params = 'default value'; } ... ... ... 

[edit: optional]

As @liquorvicar , which I also totally agree on, is that you should always declare all your class level properties / variables, regardless of whether you will use them. The reason is that you do not want to access a variable that has not been set. See my example above, which triggered an undefined property TestClass::$params error undefined property TestClass::$params ..

Thanks @liquorvicar for reminding me.

+8
source

Hope this helps you figure it out:

 class Person { private $name; private $age; public function __construct($name, $age){ // save func params to instance vars $this->name = $name; $this->age = $age; } public function say_hello(){ // look, no func params! echo "My name is {$this->name} and I am {$this->age} years old!"; } public function celebrate_birthday(){ // no params again echo "Party time!"; $this->age += 1; // update instance var $this->drink_beer(); // call instance method } public function drink_beer(){ echo "Beer is good!"; } } $p = new Person("Sample", "20"); $p->say_hello(); // My name is Sample and I am 20 years old! $p->celebrate_birthday(); // Party time! Beer is good! $p->say_hello(); // My name is Sample and I am 21 years old! 
+2
source

In general, you can use a specific variable when they are in the context ("region") of your object.

In OOP programming, $this almost always be used to access a class variable or another class method.

 class MyClass{ private $myNum; public function myFunc(){ echo 'My number is '.$this->myNum; // Outputting the class variable "myNum" } public function go($myNumber){ $this->myNum = $myNumber; // Will set the class variable "$myNum" to the value of "$myNumbe" - a parameter fo the "go" function. $this->myFunc(); // Will call the function "myFunc()" on the current object } } 

Shay.

+1
source

You yourself have answered your question:

You must set it as $ this-> params if you need to access this property in another method in this class, and you can only use $ params if you use only this property in the same method as already

+1
source

Your own theory

You must set it as $this->params if you need to access this property with another method in this class, and you can only use $params if you use this property only in the same method in which it already exists.

It is a good guide, but it is somewhat shorter. Consider

 public function applyDiscount($discount) { if ($this->isValidDiscountRangeForProduct($discount)) { $this->price -= $this->price * $discount; } } 

You cannot assign the $discount property to an object just because you need to check it before applying it to $this->price (use another method). The best guideline would be

If the argument is used only to calculate the internal state of an object, you are not making it a property.

Also, keep in mind that a method like yours

 public function TestFunc2($params) { echo 'testing this something' . $params; } 

probably wrong on this object because it has no grip. You only need methods on your object that somehow work with the data of this object. Attaching methods that do not work on object elements, similar to sticking an ice crusher onto your car:

 class Car { public function crushIce($ice) { // WTF!? } } 

Additional resources:

0
source

Source: https://habr.com/ru/post/1385304/


All Articles