Show
Suggest An AnswerIn the given tutorial, we have chosen the most common and handy ways of using self and $this in PHP. Check out the examples below and enrich your PHP arsenal with these simple and useful tricks. Let’s start at representation of the correct usage of self and $this for static and non-static member variables. Here is an example:
For the comparison, we demonstrate a wrong method of using self and $this for static and non-static member variables below:
An example of polymorphism with $this for member functions will look as follows:
An example of suppressing polymorphic behavior by applying self for member function is demonstrated below:
In PHP, $this is considered a pseudo-variable that is a reference to the current object. Most of the time, it is implemented in object-oriented codes. The variable $this is used for calling non-static methods. In case one is attempting to call a static method, then an error will occur. It means that the variable $this is now available within a static method. In PHP, the self, generally, belongs to the class members, but never for any exact object. The reason is that the static members (functions or variables) are class members that are shared by all the objects of the class. Whereas, $this refers to the member variables and function for a particular instance. The $ sign is not used for self as it doesn’t connote a variable, but the class construct. As $this doesn’t reference a particular variable, it includes a $ sign. This is the most interesting and most commonly confusing topic among developers while working on any project. In this tutorial, we’ll start understanding of So, the basic question comes in mind, what is As we know that when we create any class then that class contains constants, variables aka “properties”, and functions aka “methods”. In OOP, to access these class components, we can use either $this OR self in PHP. It means, in PHP only we can use these ways to get control over different class components. This may differ based on the programming languages you use.
What is self:: in PHP?A class may contain an amalgamation of static & non-static members. To access static members of the class, we use Static functions and variables are associated with the class itself. It means whatever the value static variables hold, it will be shared among all the other created objects. In simple words, suppose we update static variable using Let’s see code in action- <?php class books { public static $bookId = 0; public function updateBookInfo($id) { self::$bookId = $id; } public function getBookId() { return self::$bookId; } } // object 1 // before update $objBooks = new books; $objBooks->updateBookInfo(1); echo $objBooks->getBookId(); // object 2 $newObjBook = new books; echo $newObjBook->getBookId(); $newObjBook->updateBookInfo(2); // after update echo $objBooks->getBookId(); echo $newObjBook->getBookId(); // OUTPUT 1 1 2 2 Okay, so in this example, we have created a book class in which we have defined one static variable ( This method uses We can access this value using Next, we have defined another object Once, we update the value of this static variable
using So, How can we access static members of the class?So, we have seen a basic understanding of how to define static variables, how we can access it and how all instances of a class share the same static variable. There are different ways with which we can access static variable. So, static variables & functions are accessed via But from outside of the class, we need to use Sometimes we also see What is $this in PHP?
Let’s understand the $this using basic example- <?php class books { public $bookId = 0; public function setBookId($id) { $this->bookId = $id; } public function getBookId() { return $this->bookId; } } $objBook = new books; $objBook->setBookId(123); echo $objBook->getBookId(); // OUTPUT 123 As we can see, we’ve defined two methods in class So, what does But this is NOT true. Yes, that’s true. We can also add new variable(s) dynamically for temporary use to the class. You may be confused now because we’ve seen so many examples which contains use of properties defined within class. Let’s use the same example of book class and define dynamic variables defined outside of the class. <?php class books { public $bookId = 0; public function setBookId($id) { $this->bookId = $id; } public function getBookId() { return $this->bookId; } public function getCurrentObjInfo() { print_r($this); } } $objBook = new books; $objBook->setBookId(123); $objBook->name = "Jack"; echo $objBook->getCurrentObjInfo(); // OUTPUT books Object ( [bookId] => 123 [name] => Jack ) We’ve defined a new method So, when we called this new method Now, let’s discuss the main topic. When to use self:: in PHP?This the biggest question everyone faced while developing any software. Which class methods / properties should be static and WHY? In the above, we’ve seen that To understand this let’s create a person class and then we will see how using static properties/methods can be useful. <?php class Person { private $name = ''; private $age = ''; private $address = ''; private $familyMembers = ''; public static $votingAge = 18; public function __construct($name, $age, $address, $familyMembers) { $this->name = $name; $this->age = $age; $this->height = $address; $this->weight = $familyMembers; } public function get_name() { return $this->name; } public function get_age() { return $this->age; } public function get_address() { return $this->address; } public function get_family_members() { return $this->familyMembers; } public static function set_voting_age($age) { self::$votingAge = $age; } public static function get_voting_age() { return self::$votingAge; } } So, basically we have defined Person class and also added four properties name, age, address & familyMembers. We then created static variable votingAge because declaring votingAge static, we are making sure that every object created using Person class should contain the same static votingAge value. This was we make sure that even if we created as many objects of person class as possible but all the objects will share the same votingAge. Another advantage of creating static property OR making When to use $this in PHP?In contrast to when to use self:: in PHP, using $this only useful when we actually need to create an object of the class to define and access the non-static members of the class. $this is using inside the class methods only. So once object of class (e.g., Person) is created then to access all the members we need to use $this pseudo-variable. <?php class Person { private $name = ''; public function __construct($name) { $this->name = $name; } public function get_name() { return $this->name; } We have reduced above example and just used get_name() function to access name of the person. As this property name can have different name depending upon the value passed to the constructor. So, to access this property we need to use $this variable. In a nutshell, lets see the difference between self:: and $this. Difference in self and $this in PHP
Conclusion:We have seen in-depth understanding of self and $this in PHP. So, basically, we need to make sure that while creating any class, what is the purpose of this class and how different components of the class will help in making that class complete. Once we made our decision final then we can use appropriately when to use self and when to use $this. Is self and $this same in PHP?self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.
What is difference between self and static in PHP?PHP new self vs new static: Now that we changed the code in our example to use static instead of self, you can see the difference is that self references the current class, whereas the static keyword allows the function to bind to the calling class at runtime.
What is $this in PHP?$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.
Is self and this keyword same?Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.
|