When will you use this $ keyword in PHP?

When do you use the $this in PHP? From what I understand, $this refers to an object created without knowing the name of the object.

Also, the keyword $this can only be used in a method?

Will give an example when you can use $this .

+8
oop php
source share
12 answers

A class can contain its own constants, variables (called "properties") and functions (called "methods").

 <?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } ?> 

Some examples of the $ this pseudo-variable:

 <?php class A { function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } } } class B { function bar() { // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); } } $a = new A(); $a->foo(); // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); $b = new B(); $b->bar(); // Note: the next line will issue a warning if E_STRICT is enabled. B::bar(); ?> 

The above example outputs:

  • $ this is defined (A)
  • $ this is undefined.
  • $ this is defined (B)
  • $ this is undefined.
+6
source share

The most common use case is in object-oriented programming, defining or working inside a class. For example:

 class Horse { var $running = false; function run() { $this->running = true; } } 

As you can see, in the run function we can use the $this variable to refer to the instance of the Horse class that we are in. Therefore, it should be borne in mind that if you create 2 classes of horses, the $this variable inside each will refer to this particular instance of the Horse class, and not to both of them.

+3
source share

You would only use $ this if you are doing object oriented programming in PHP. Value if you create classes. Here is an example:

 class Item { protected $name, $price, $qty, $total; public function __construct($iName, $iPrice, $iQty) { $this->name = $iName; $this->price = $iPrice; $this->qty = $iQty; $this->calculate(); } } 
+2
source share

$ is used to create a link to the current instance of the object. This way you can do things like:

 class MyClass { private $name; public function setName($name) { $this->name = $name; } //vs public function setName($pName) { $name = $pName; } } 

It is also useful to use the following methods:

 class MyClass2 { private $firstName; private $lastName; public function setFirstName($name) { $this->firstName = $name; return $this; } public function setLastName($name) { $this->lastName = $name; return $this; } public function sayHello() { print "Hello {$this->firstName} {$this->lastName}"; } } //And now you can do: $newInstance = new MyClass2; $newInstance->setFirstName("John")->setLastName("Doe")->sayHello(); 
+2
source share

It is used in object-oriented programming (OOP):

 <?php class Example { public function foo() { //code } public function bar() { $this->foo(); } } 

The pseudo-variable $ this is available when the method is called from within the object context. $ is a reference to the calling object (usually the object to which the method belongs, but possibly another object if the method is called statically from the context of the secondary object).

+1
source share

Once I know that the ultimate use of the this equivalent in other languages ​​is to implement the Free interface; each class method that would otherwise return void instead returns this , so method calls can be easily chained together.

 public function DoThis(){ //Do stuff here... return $this; } public function DoThat(){ //do other stuff here... return $this; } 

The above could be called so:

 myObject->DoThis()->DoThat(); 

Which may be useful for some things.

+1
source share

Used when you want to work with local variables.

You can also read about it from here .

 function bark() { print "{$this->Name} says Woof!\n"; } 
+1
source share

$ this is used when you create a new instance of the object.

For example, imagine this:

 class Test { private $_hello = "hello"; public function getHello () { echo $this->_hello; // note that I removed the $ from _hello ! } public function setHello ($hello) { $this->_hello = $hello; } } 

To access the getHello method, I have to create a new instance of the Test class, for example:

 $obj = new Test (); // Then, I can access to the getHello method : echo $obj->getHello (); // will output "hello" $obj->setHello("lala"); echo $obj->getHello (); // will output "lala" 

In fact, $ this is used inside the class when this is not possible. This is called an area .

Inside your class, you use $ this (to access * $ _ hello *, for example), but outside the class, $ this does NOT apply to elements inside your class (e.g. * $ _ hello *), this is what $ obj does.

Now the main difference between $ obj and $ is that $ obj gets access to your class from outside , some restrictions occur: if you define something private or protected in your class, for example, my variable * $ _ hello *, $ obj CANNOT access it (it's private!), but $ this can becase $ this to leave inside the class.

+1
source share

No, I think ur idea is wrong .. $this used when referring to an object of the same class .. like this

we think that we have a variable value of $ var and 5 should be set in the THAT instance of this object

$ this-> variable = 5;

0
source share

Using $ this refers to reference methods or instance variables belonging to the current object

$ this-> name = $ name or $ This-> callSomeMethod ()

which will use a variable or method implemented in the current object by a subclass or not.

If you want to specifically call the implementation of the parent class, you would do something like

Parent :: callSomeMethod ()

0
source share

Whenever you want to use a variable that is outside the function, but inside the same class, you use $ this. This is the basic concept of php.

0
source share
 <?php class identity { public $name; public $age; public function display() { return $this->name . 'is'. $this->age . 'years old'; } } ?> 
-one
source share

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


All Articles