PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP Object Oriented ProgrammingPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP Interfaces

PHP OOP $this Keyword

$this refers to the current object.

$this is a pseudo-variable (also a reserved keyword) which is only available inside methods. And, it refers to the object of the current method.

Let's create a House class with one property (color). Then, create an object from it.


<?php
class House {
	public $color = 'black';
}
$house = new House();

Where to use $this?

How can we change the value of $color? Take some time and think.

There are two ways.

  1. Outside the class. We can directly change the value of a property from outside of the class.
    
    $house -> color = 'white';
    
    
  2. Inside the class. We can define a method in the class and call it to change the value of its own property.
    
    public function changeColor() {
    	$this -> color = 'white';
    }
    
    
    
    $house -> changeColor();
    
    

In the second way, we use $this variable to access the current object. Here, the current object is the object saved in $house.

More Examples

Let's play with the $this pseudo-variable.

Here we will create two houses and assign names and colors to them. Then, we will echo it out.

  1. Create the House class
    
    <?php
    class House {
    	public $name;
    	public $color;
    	public function setData($name, $color) {
    		$this -> name = $name;
    		$this -> color = $color;
    	}
    	public function echoData() {
    		echo "The color of the {$this -> name} is {$this -> color}";
    	}
    }
    
    
  2. Create two objects from it
    
    $blackHouse = new House();
    $whiteHouse = new House();
    
    
  3. Call the setData method with two arguments
    
    $blackHouse -> setData("John's House", "black");
    $whiteHouse -> setData("Pearl's House", "white");
    
    
  4. Call the echoData to echo out a descriptive phrase
    
    $blackHouse -> echoData();
    echo '<br>';
    $whiteHouse -> echoData();
    
    

Here's the full code.

PHP OOP $this Keyword Example


<?php
class House {
	public $name;
	public $color;
	public function setData($name, $color) {
		$this -> name = $name;
		$this -> color = $color;
	}
	public function echoData() {
		echo "The color of the {$this -> name} is {$this -> color}";
	}
}

$blackHouse = new House();
$whiteHouse = new House(); // this is a small house, not america's one ;)

$blackHouse -> setData("John's House", "black");
$whiteHouse -> setData("Pearl's House", "white");
 
$blackHouse -> echoData();
echo '<br>';
$whiteHouse -> echoData();

Run Example ››

Conclusion

We could access the current object with $this keyword. But, we needed to run two steps of code to create a object ($house = new House()) and set its data ($house -> setData(...)). What if we could set the data when we create the object? We can run a constructor function when the object is created. You can learn more in the next chapter.

Facebook Twitter