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
The examples have very different outcomes.
Before looking at the differences, the following should be noted:
- A constructor's prototype provides a way to share methods and values among instances via the instance's private
[[Prototype]]
property.
- A function's this is set by how the function is called or by the use of bind (not discussed here). Where a function is called on an object (e.g.
myObj.method()
) then this within the method references the object. Where this is not set by the call or by the use of bind, it defaults to the global object (window in a browser) or in strict mode, remains undefined.
- JavaScript is an object-oriented language, i.e. most values are objects, including functions. (Strings, numbers, and booleans are not objects.)
So here are the snippets in question:
var A = function () {
this.x = function () {
//do something
};
};
In this case, variable A
is assigned a value that is a reference to a function. When that function is called using A()
, the function's this isn't set by the call so it defaults to the global object and the expression this.x
is effective window.x
. The result is that a reference to the function expression on the right-hand side is assigned to window.x
.
In the case of:
var A = function () { };
A.prototype.x = function () {
//do something
};
something very different occurs. In the first line, variable A
is assigned a reference to a function. In JavaScript, all functions objects have a prototype property by default so there is no separate code to create an A.prototype object.
In the second line, A.prototype.x is assigned a reference to a function. This will create an x property if it doesn't exist, or assign a new value if it does. So the difference with the first example in which object's x property is involved in the expression.
Another example is below. It's similar to the first one (and maybe what you meant to ask about):
var A = new function () {
this.x = function () {
//do something
};
};
In this example, the new
operator has been added before the function expression so that the function is called as a constructor. When called with new
, the function's this is set to reference a new Object whose private [[Prototype]]
property is set to reference the constructor's public prototype. So in the assignment statement, the x
property will be created on this new object. When called as a constructor, a function returns its this object by default, so there is no need for a separate return this;
statement.
To check that A has an x property:
console.log(A.x) // function () {
// //do something
// };
This is an uncommon use of new since the only way to reference the constructor is via A.constructor. It would be much more common to do:
var A = function () {
this.x = function () {
//do something
};
};
var a = new A();
Another way of achieving a similar result is to use an immediately invoked function expression:
var A = (function () {
this.x = function () {
//do something
};
}());
In this case, A
assigned the return value of calling the function on the right-hand side. Here again, since this is not set in the call, it will reference the global object and this.x
is effective window.x
. Since the function doesn't return anything, A
will have a value of undefined
.
These differences between the two approaches also manifest if you're serializing and de-serializing your Javascript objects to/from JSON. Methods defined on an object's prototype are not serialized when you serialize the object, which can be convenient when for example you want to serialize just the data portions of an object, but not it's methods:
var A = function () {
this.objectsOwnProperties = "are serialized";
};
A.prototype.prototypeProperties = "are NOT serialized";
var instance = new A();
console.log(instance.prototypeProperties); // "are NOT serialized"
console.log(JSON.stringify(instance));
// {"objectsOwnProperties":"are serialized"}
Related questions:
Sidenote: There may not be any significant memory savings between the two approaches, however using the prototype to share methods and properties will likely use less memory than each instance having its own copy.
JavaScript isn't a low-level language. It may not be very valuable to think of prototyping or other inheritance patterns as a way to explicitly change the way memory is allocated.
Best Answer
With ECMAScript5's
Function.prototype.bind
things get pretty clean:It can be used as follows:
or even directly:
This and the eval-based solution are the only ones that always work, even with special constructors like
Date
:edit
A bit of explanation: We need to run
new
on a function that takes a limited number of arguments. Thebind
method allows us to do it like so:The
anything
parameter doesn't matter much, since thenew
keyword resetsf
's context. However, it is required for syntactical reasons. Now, for thebind
call: We need to pass a variable number of arguments, so this does the trick:Let's wrap that in a function.
Cls
is passed as argument 0, so it's gonna be ouranything
.Actually, the temporary
f
variable is not needed at all:Finally, we should make sure that
bind
is really what we need. (Cls.bind
may have been overwritten). So replace it byFunction.prototype.bind
, and we get the final result as above.