Home > Backend Development > PHP Tutorial > What's the Difference Between `self` and `$this` in PHP 5 Object-Oriented Programming?

What's the Difference Between `self` and `$this` in PHP 5 Object-Oriented Programming?

DDD
Release: 2024-12-17 16:26:17
Original
360 people have browsed it

What's the Difference Between `self` and `$this` in PHP 5 Object-Oriented Programming?

Understanding the Difference Between 'self' and '$this' in PHP 5

When working with object-oriented programming in PHP 5, it's crucial to grasp the distinctions between using 'self' and '$this' effectively. Both are references but serve distinct purposes in object interaction.

'$this' - Referring to the Current Object

Use '$this' to access non-static member variables and methods within the current instance of your object. It provides a direct pointer to the specific object being instantiated. '$this->member' syntax allows you to access non-static variables, while '$this->method()' invokes instance methods.

Example:

class Person {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

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

In this example, '$this' is used to access the 'name' property and 'getName()' method within the 'Person' object.

'self' - Referring to the Current Class

In contrast, 'self' is employed to access static members and methods within the current class. It refers to the class itself, not a specific instance of the class. 'self::$static_member' syntax enables access to static variables, while 'self::static_method()' calls class methods.

Example:

class StaticCounter {
    private static $count = 0;

    public static function incrementCount() {
        self::$count++;
    }

    public static function getCount() {
        return self::$count;
    }
}
Copy after login

Here, 'self' is utilized to access the static 'count' variable and 'incrementCount()' class method within the 'StaticCounter' class.

Conclusion

Understanding the appropriate usage of 'self' and '$this' is vital for effective object-oriented programming in PHP 5. '$this' targets the current object's non-static members, while 'self' focuses on the current class's static members. By mastering these distinctions, you can enhance your code clarity and functionality when working with classes and objects.

The above is the detailed content of What's the Difference Between `self` and `$this` in PHP 5 Object-Oriented Programming?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template