this and self in PHP

Olorunfemi Akinlua Jul 01, 2022
  1. OOP in PHP
  2. this and self in PHP
this and self in PHP

this and self are component properties of Object-Oriented Programming (OOP). OOP is a component feature of PHP as a programming procedure rather than procedural programming, where we write functions that perform data operations.

With OOP, we can create objects that have both data and functions (methods).

Though, OOP provides a faster and more comprehensive way to code in any language that supports it, including PHP. Certain features or properties can be complicated, such as this and self and can make using OOP not fun.

This article will discuss how this and self are different and how to use them in PHP.

OOP in PHP

OOP provides a clear structure for your PHP programs and allows you to follow the popular principle “Don’t Repeat Yourself”.

Classes and Methods are a big part of OOP in PHP and are easily created using the following code snippet.

<?php

class Good {

    public $propertyOne;
    public $propertyTwo;
    private $propertyThree;

    function methodOne($propertyOne) {
        //
    }
}

?>

The $propertyOne, $propertyTwo, $propertyThree are properties of the class Good and methodOne() is a method.

We can create objects, the entire goal of OOP, and the reason for the classes and methods, by using this code snippet.

$goodOne = new Good();

this and self in PHP

To expand the current code, we can create a method to set the $propertyOne of the class Good.

class Good {

		//...

		function setGoodName($propertyOne) {
        $this->propertyOne = $propertyOne;
    }

    function showGoodName() {
        return $this->propertyOne;
    }

}

The $this keyword refers to the current object and is only available inside methods within a class. So, if we want to use $this in our PHP code, it must be inside a method within our class.

In the case of the code snippet, the $this keyword points to the current object to allow us to call the $propertyOne within the Good class.

Let’s make use of the methods we have created.

$book = new Good();
$book->setGoodName("PHP for Dummies");

echo $book->showGoodName();

The output of the code snippet is below.

PHP for Dummies

Let’s extend our PHP code further and add a property to the class Good about the static store Name, make it private, and return the property to a constructor.

class Good {

		//...
    private static $storeName = "JK Book Store";

    function __construct()
    {
        return self::$storeName;
    }

		//...
}

The self keyword refers to the current class and allows us to access class and static variables as in the above code snippet. The self keyword uses the scope resolution operator :: to access or refer to static class members.

Therefore, a big difference between self and $this is that self accesses static or class variables or methods and $this accesses non-static and object variables and methods.

So, when working with OOP, know that we use $this inside objects (instance of a class) and self for classes and static properties.

Full Source Code:

<?php

class Good {

		// properties
    public $propertyOne;
    public $propertyTwo;
    private $propertyThree;
    private static $storeName = "JK Book Store";

		// methods
    function __construct()
    {
        return self::$storeName;
    }

    function setGoodName($propertyOne) {
        $this->propertyOne = $propertyOne;
    }

    function showGoodName() {
        return $this->propertyOne;
    }

}

// creating a object
$book = new Good();
$book->setGoodName("PHP for Dummies");

echo $book->showGoodName();

?>
Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn

Related Article - PHP Class