PHP OOPs Concepts

The $this keyword

As an example, we wrote a Car class that groups all the code that handles cars.

class Car {
public $comp;
public $numWheels = 4;
public $hasSunRoof = true;
public function hello()
{
return "beep";
}
}

We also created two objects out of the class in order to be able to use its code:

$bmw = new Car ();
$mercedes = new Car ();

The $this keyword indicates that we use the class’s own methods and properties, and allows us to have access to them within the class’s scope.
The $this keyword allows us to approach the class properties and methods from within the class using the following syntax:

$this -> propertyName;
$this -> methodName();

• Only the this keyword starts with the $ sign, while the names of the properties and methods do not start with it.
The $this keyword indicates that we use the class’s own methods and properties, and allows us to have access to them within the class’s scope.
Let’s illustrate what we have just said on the Car class. We will enable the hello() method to approach the class’s own properties by using the $this keyword.
We use:

$this -> comp;

in order to approach the class $comp property. We also use:

$this -> color;

in order to approach the class $color property.
That’s what the code looks like:

class Car {
// The properties
public $comp;
public $color = "beige";
public $hasSunRoof = true;
// The method can now approach the class properties
// with the $this keyword
public function hello()
{
return "Beep I am a <i>" . $this -> comp . "</i>, and I am <i>" .
$this -> color;
}
}

Let us create two objects from the class:

$bmw = new Car();
$mercedes = new Car ();

and set the values for the class properties:

$bmw -> comp = "BMW";
$bmw -> color = "blue";
$mercedes -> comp = "Mercedes Benz";
$mercedes -> color = "green";

We can now call the hello method for the first car object:

echo $bmw -> hello();
Result

Beep I am a BMW, and I am blue.
And for the second car object:

echo $mercedes -> hello();
Result

Beep I am a Mercedes Benz, and I am green.
This is the full code that we have written in this Tutorial:

class Car {
// The properties
public $comp;
public $color = "beige";
public $hasSunRoof = true;
// The method that says hello
public function hello()
{
return "Beep I am a <i>" . $this -> comp .
"</i>, and I am <i>" . $this -> color ;
}
}
// We can now create an object from the class
$bmw = new Car();
$mercedes = new Car();
// Set the values of the class properties
$bmw -> color = "blue";
$bmw -> comp = "BMW";
$mercedes -> comp = "Mercedes Benz";
// Call the hello method for the the $bmw object
echo $bmw -> hello();

Recommended Tutorials for you


Related posts

Traits and code inclusion

Anuj Kumar

Inheritance in PHP

Anuj Kumar

Abstract classes and methods

Anuj Kumar

OOPs Concept in Php – Offering A Smarter Future for Users

Anuj Kumar

How to Insert Data in Database using PHP OOPS Concept

Anuj Kumar

Basics Of OOPs in PHP

Anuj Kumar

How to Fetch data from database Using OOPS In PHP

Anuj Kumar

Static methods and properties

Anuj Kumar

Interfaces – the next level of abstraction

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.