When should I use 'self' over '$this'?
Short Answer
Use
$this
to refer to the current
object. Useself
to refer to the
current class. In other words, use
$this->member
for non-static members,
useself::$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 thefoo()
member function of whatever is the exact type of the current object. If the object is oftype X
, it thus callsX::foo()
. If the object is oftype Y
, it callsY::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:
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.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
How to Get File_Get_Contents() to Work With Https
MySQL VS MySQLi When Using PHP
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Creating the Singleton Design Pattern in PHP5
Best Way to Get Result Count Before Limit Was Applied
MySQL Connection Not Working: 2002 No Such File or Directory
Automated or Regular Backup of MySQL Data
Return Single Column from a Multi-Dimensional Array
500 Internal Server Error For PHP File Not For Html
How to Set Up File Permissions For Laravel
How to Reindex an Array in PHP But With Indexes Starting from 1
PHP - Floating Number Precision
How to With MySQLi Make a Query With Like and Get All Results