Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use 'self' over '$this'?

Tags:

scope

oop

php

class

In PHP 5, what is the difference between using self and $this?

When is each appropriate?

like image 298
Casey Watson Avatar asked Sep 30 '08 06:09

Casey Watson


People also ask

What is the difference between using self and this?

The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.

What is the use of self in laravel?

self is for use in static member functions to allow you to access static member variables. $this is used in non-static member functions, and is a reference to the instance of the class on which the member function was called.

What does self mean in PHP?

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 objecxts of the class.

Why we use $this in PHP?

$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.


2 Answers

Short Answer

Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

Full Answer

Here is an example of correct usage of $this and self for non-static and static member variables:

<?php class X {     private $non_static_member = 1;     private static $static_member = 2;      function __construct() {         echo $this->non_static_member . ' '            . self::$static_member;     } }  new X(); ?> 

Here is an example of incorrect usage of $this and self for non-static and static member variables:

<?php class X {     private $non_static_member = 1;     private static $static_member = 2;      function __construct() {         echo self::$non_static_member . ' '            . $this->static_member;     } }  new X(); ?> 

Here is an example of polymorphism with $this for member functions:

<?php class X {     function foo() {         echo 'X::foo()';     }      function bar() {         $this->foo();     } }  class Y extends X {     function foo() {         echo 'Y::foo()';     } }  $x = new Y(); $x->bar(); ?> 

Here is an example of suppressing polymorphic behaviour by using self for member functions:

<?php class X {     function foo() {         echo 'X::foo()';     }      function bar() {         self::foo();     } }  class Y extends X {     function foo() {         echo 'Y::foo()';     } }  $x = new Y(); $x->bar(); ?> 

The idea is that $this->foo() calls the foo() member function of whatever is the exact type of the current object. If the object is of type X, it thus calls X::foo(). If the object is of type Y, it calls Y::foo(). But with self::foo(), X::foo() is always called.

From http://www.phpbuilder.com/board/showthread.php?t=10354489:

By http://board.phpbuilder.com/member.php?145249-laserlight

like image 153
John Millikin Avatar answered Oct 19 '22 09:10

John Millikin


The keyword self does NOT refer merely to the 'current class', at least not in a way that restricts you to static members. Within the context of a non-static member, self also provides a way of bypassing the vtable (see wiki on vtable) for the current object. Just as you can use parent::methodName() to call the parents version of a function, so you can call self::methodName() to call the current classes implementation of a method.

class Person {     private $name;      public function __construct($name) {         $this->name = $name;     }      public function getName() {         return $this->name;     }      public function getTitle() {         return $this->getName()." the person";     }      public function sayHello() {         echo "Hello, I'm ".$this->getTitle()."<br/>";     }      public function sayGoodbye() {         echo "Goodbye from ".self::getTitle()."<br/>";     } }  class Geek extends Person {     public function __construct($name) {         parent::__construct($name);     }      public function getTitle() {         return $this->getName()." the geek";     } }  $geekObj = new Geek("Ludwig"); $geekObj->sayHello(); $geekObj->sayGoodbye(); 

This will output:

Hello, I'm Ludwig the geek
Goodbye from Ludwig the person

sayHello() uses the $this pointer, so the vtable is invoked to call Geek::getTitle(). sayGoodbye() uses self::getTitle(), so the vtable is not used, and Person::getTitle() is called. In both cases, we are dealing with the method of an instantiated object, and have access to the $this pointer within the called functions.

like image 36
nbeagle Avatar answered Oct 19 '22 07:10

nbeagle