When Should I Use 'Self' Over '$This'

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

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

using self instead of this variable

When a class is called statically ie. ClassName::someMethod(), there is no "instance" of the class.

Since $this refers to the instance of the class, $this will not exist when your class is used statically. (so $this will only be available when you created an object of your class by using $var = new ClassName())

self refers to the class (not the object) so in static classes you can use self::.. to refer to properties or methods within the class.

Using $this inside a static function fails

This is the correct way

public static function userNameAvailibility()
{
$result = self::getsomthin();
}

Use self:: instead of $this-> for static methods.

See: PHP Static Methods Tutorial for more info :)

Using $this when not in object context php

There are two problems here:

  1. You have defined your methods as static. You should not do that as they are not, they depend on being called on an object as you want to use the objects non-static properties.

  2. You have a typo in your constructor function. The correct name for the constructor is __construct, notice the two _ at the beginning.

Why can't we use 'this' keyword in a static method

Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use

class Sub {
static int y;
public static void foo() {
y = 10;
}
}

If you want to make sure you get the static field y and not some local variable with the same name, use the class name to specify:

class Sub {
static int y;
public static void foo(int y) {
Sub.y = y;
}
}

call a static method inside a class?

self::staticMethod();

More information about the Static keyword.

What is the difference between self and $this in php?

$this refers to the current instance. self refers to the current class.

In other words, you can use $this->someMember to refer to an instance member and self::$someStaticMember to refer to a static member.



Related Topics



Leave a reply



Submit