While setting up a test server for some software I wrote at the office, I eventually noticed the following notice:
Notice: Use of undefined constant self - assumed 'self'
That surprised me, because 1) I though self were some kind of "superglobal" constant or a special token of the parser, always automatically available in a static method and 2) the code works. So what's up in there ? Let's make a simple test:
<?php
class foo {
static public function bar() {
var_dump(is_callable(array('self', 'foobar')));
var_dump(is_callable(array(self, 'foobar')));
var_dump(class_exists('self'));
var_dump(class_exists(self));
self::foobar();
}
static public function foobar() {
}
}
foo::bar();
Executing the above code will yeld the following result:
bool(true)
Notice: Use of undefined constant self - assumed 'self' in /home/geoffreyb/test.php on line 6
bool(true)
bool(false)
Notice: Use of undefined constant self - assumed 'self' in /home/geoffreyb/test.php on line 9
bool(false)
What do we learn here ? Not much actually. It seems like self as a constant is only available when used with the scope resolution operator, aka double-colon or paamayim nekudotayim. When you want to use it in, for example, a callback definition, use a string representation of self:
<?php
is_callable(array('self', 'bar'));
call_user_func(array('self', 'bar'));
Which, while making absolutely no sense at all, works. Another way to get around this is to use the get_class() function that, without any argument, will return the name of the class you're currently in (foo in my example).
After a bit more investiging, I found out that there is nothing special about the self token, which is actually a string token. You can check this very easily with the following code:
<?php
class foo {
static public function bar() {
self::foobar();
}
}
var_dump(token_get_all(file_get_contents(__FILE__)));
Somewhere inside the output, you'll find the following piece of text:
array(2) {
[0]=>
int(307)
[1]=>
string(4) "self"
}
And the token id 307 is resolved by token_name to string.