So here is an embedded voicemail from google voice, which manages all my sms, and voicemails. very slick.
Monday, August 10, 2009
Monday, August 3, 2009
self:: vs this->
Here is an example of correct usage of $this and self for non-static and static member variables:
Here is an example of incorrect usage of $this and self for non-static and static member variables:
Here is an example of polymorphism with $this for member functions:
Here is an example of suppressing polymorphic behaviour by using self for member functions:
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.
PHP Code:
class X {
private $non_static_member = 1;
private static $static_member = 2;
function __construct() {
echo $this->non_static_member . ' '
. self::$static_member;
}
}
new X();
?>
PHP Code:
class X {
private $non_static_member = 1;
private static $static_member = 2;
function __construct() {
echo self::$non_static_member . ' '
. $this->static_member;
}
}
new X();
?>
PHP Code:
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();
?>
PHP Code:
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();
?>
Subscribe to:
Posts (Atom)