This Static and Self keyword in OOP example with PHP

OOP
Spread the love

The $this keyword denoted the current object and it is available inside a class. $this is a reserve keyword, which is allowed you to access the properties and methods of the current object within the class using the object operator (→).

Let’s have an example to see how we use $this keyword.

class User
{
private $name;
public $age;

public function getName()
{
return $this->name;
}
}

In this User class, there are two properties and a method. Inside the (getName) method, I used $this keyword to access the name property. Not only properties, but you can also use any non-static method inside another method using $this keyword. Let’s see this example.

class User
{
private $name;
public $age;

public function getName()
{
return $this->name;
}

public function getDetails()
{
return "{$this->getName()} {$this->age}";
}
}

Well, look at the getDetails method. In this method, I called the getName method which is a non-static method.

How does this method work?

Well, I told you before that the $this keyword return the current object. Let’s see the following code, then I will explain

class User
{
public $name;
public $age;

public function getObject()
{
return $this;
}
}

$ob = new User();

var_dump($ob->getObject());

Output :

Well, In this code I defined a method that returns $this, then I dumped the output of this function. You can see the $this keyword return the current object’s properties. Well, So I proved that the $this method return the current object.

Well, using this concept you can chain method. Method chaining is a concept where you can call multiple methods without using an instance of an object. To do that, your method should return a current object that we get in PHP using the $this keyword. Let’s see the example.

class User
{
public $name;
public $age;

public function methodOne()
{
// TODO : Some task 
return $this;
}
public function methodTwo()
{
// TODO : Some task 
return $this;
}
public function methodThree()
{
// TODO : Some task 
echo "Method Chaining!!!!";
}
}

$ob = new User();

$ob->methodOne()->methodTwo()->methodThree();

Output

Well, In User class I defined a couple of methods. They are mehtodOne, methodTwo and methodThree. Method one and two do some tasks then return $this ( the current object). And finally method three I have printed a massage. Well, now I called methodOne then call methodTwo then finally called method three and get output. So without using an instance we can get access the method using method chaining.

The static keyword

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class.

The static keyword is also used to declare variables in a function that keep their value after the function has ended. Let’s have an example

class User
{
public static $name = "10minutesCode";
public static $address = "NULL";

public static function getAddress()
{
return self::$name . " " . self::$address;
}
}

echo User::getAddress(); // Output : 10minutesCode NULL

Well, in this class I defined two static properties and a static method. Inside the method, I return the properties using the self keyword instead of the $this keyword. Remember to access the static properties or method you have to use self instead of $this and you have to use (::) scope resolution operator. After then I call the getAddress method without creating an instance.

Difference between PHP self and this in OOP

Let’s understand a few differences between self and this:

selfthis
self keyword is not preceded by any symbol.this keyword should be preceded with a $ symbol.
To access class variables and methods using the self keyword, we use the scope resolution operator ::In case of this operator, we use the -< symbol.
It is used to refer the static members of the class.It is used to access non-static members of the class.
PHP self refers to the class members, but not for any particular object. This is because the static members(variables or functions) are class members shared by all the objects of the class.Whereas, $this will refer to the member variables and function for a particular instance.

Read More: Class and Object in OOP example with PHP


Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m an Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://tutspack.com

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.