$this keyword in PHP Last Updated : 09 Jul, 2020 Comments Improve Suggest changes Like Article Like Report $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. Example 1: A simple program to show the use of $this in PHP. php <?php class simple{ public $k = 9; public function display(){ return $this->k; } } $obj = new simple(); echo $obj->display(); ?> Output: 9 $this - a pseudo-variable: Unlike other reserved keywords used in the context of class like the static, parent, etc does not need to be declared with the dollar sign ('$'). This is because in PHP $this is treated as a pseudo-variable. In PHP, this is declared like a variable declaration (with the '$' sign) even though it is a reserved keyword. More specifically, $this is a special read-only variable that is not declared anywhere in the code and which represents a value that changes depending on the context of program execution. Example 2: A program that updates the value of a variable of a specific object using $this keyword. php <?php class simple { public $k = 9; public function change($val){ $this->k = $val; } public function display(){ return $this->k; } } $obj = new simple(); print("value of before update: "); echo $obj->display(); $obj->change(8); print("value of after update: "); echo $obj->display(); ?> Output: value of before update: 9 value of after update: 8 As of PHP 7.0.0, calling a non-static method statically from an incompatible context results in $this being "undefined" to the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been deprecated (even if called from a compatible context). Before PHP 5.6.0, such calls already triggered a strict notice. Example 3: In this example, $this keyword becomes "not defined" when a non-static method is called in the context of a static one. php <?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() { A::foo(); } } $a = new A(); $a->foo(); A::foo(); $b = new B(); $b->bar(); B::bar(); ?> Output: $this is defined (A) $this is not defined. $this is not defined. $this is not defined. Comment More infoAdvertise with us Next Article $this keyword in PHP V vanshikagoyal43 Follow Improve Article Tags : Web Technologies PHP PHP-basics Similar Reads PHP key() Function The key() function is an inbuilt function in PHP which is used to return the index of the element of a given array to which the internal pointer is currently pointing. The current element may be starting or next element which depends on the cursor position. By default cursor position is at zero inde 2 min read PHP echo and print PHP echo and print are two most language constructs used for output data on the screen. They are not functions but constructs, meaning they do not require parentheses (though parentheses can be used with print). Both are widely used for displaying strings, variables, and HTML content in PHP scripts. 4 min read PHP | $ vs $$ operator The $ operator in PHP is used to declare a variable. In PHP, a variable starts with the $ sign followed by the name of the variable. For example, below is a string variable: $var_name = "Hello World!"; The $var_name is a normal variable used to store a value. It can store any value like integer, flo 2 min read Introduction to PHP8 Back in the mid-1990s, PHP started as a Personal Home Page, but now it's known as Hypertext Preprocessor. It's a widely used scripting language that is perfect for web development and can easily be inserted into HTML. Over time, PHP has become super powerful for making dynamic and engaging web apps. 5 min read PHP 7 | Spaceship Operator This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by "". This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands. This ope 2 min read PHP stristr() Function The stristr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-insensitive. Syntax : strist 2 min read PHP | Interface An Interface allows the users to create programs, specifying the public methods that a class must implement, without involving the complexities and details of how the particular methods are implemented. It is generally referred to as the next level of abstraction. It resembles the abstract methods, 3 min read PHP | time() Function The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch. The number of seconds can be converted to the current date using date() function in PHP. Syntax: int time() Parameter: This function does not accepts any parameter 2 min read PHP SplFixedArray key() Function The SplFixedArray::key() function is an inbuilt function in PHP which is used to get the key of the current index of the array. Syntax: int SplFixedArray::key() Parameters: This function does not accept any parameter. Return Value: This function returns the key of the current index of the array. Bel 1 min read PHP key_âexists() Function The key_exists() function is an inbuilt function in PHP that is used to check whether the given key exist in the given array or not. If given key exist in the array then it returns true otherwise returns false. This function is an alias of array_key_exists() function. Syntax: bool key_exists(string| 2 min read Like