<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://mirmodynamics.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Mirmo Dynamics - Tag - weird</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/weird/rss2" rel="self" type="application/rss+xml"/>
  <description>Si tu kiffes pas reunoi, t'écoutes pas et puis c'est tout.</description>
  <language>en</language>
  <pubDate>Sun, 14 Mar 2010 19:59:01 +0100</pubDate>
  <copyright>2003-2009 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>About the self keyword in static methods</title>
    <link>http://mirmodynamics.com/post/2008/07/14/About-the-self-keyword-in-static-methods</link>
    <guid isPermaLink="false">urn:md5:610999c6e714c4d650b1d2ea45ffc0b3</guid>
    <pubDate>Mon, 14 Jul 2008 16:04:00 +0200</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>callback</category><category>nonsense</category><category>php</category><category>self</category><category>static</category><category>weird</category>    
    <description>    &lt;p&gt;While setting up a test server for some software I wrote at the office, I eventually noticed the following notice:&lt;/p&gt;

&lt;pre&gt;
Notice: Use of undefined constant self - assumed 'self'
&lt;/pre&gt;


&lt;p&gt;That surprised me, because 1) I though self were some kind of &amp;quot;superglobal&amp;quot; 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:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?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();

&lt;/pre&gt;


&lt;p&gt;Executing the above code will yeld the following result:&lt;/p&gt;

&lt;pre&gt;
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)
&lt;/pre&gt;


&lt;p&gt;What do we learn here ? Not much actually. It seems like &lt;code&gt;self&lt;/code&gt; 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:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?php

is_callable(array('self', 'bar'));
call_user_func(array('self', 'bar'));
&lt;/pre&gt;


&lt;p&gt;Which, while making absolutely no sense at all, works. Another way to get around this is to use the &lt;code&gt;get_class()&lt;/code&gt; function that, without any argument, will return the name of the class you're currently in (&lt;code&gt;foo&lt;/code&gt; in my example).&lt;/p&gt;


&lt;p&gt;After a bit more investiging, I found out that there is nothing special about the &lt;em&gt;self&lt;/em&gt; token, which is actually a string token. You can check this very easily with the following code:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?php

class foo {
	static public function bar() {
		self::foobar();
	}
}

var_dump(token_get_all(file_get_contents(__FILE__)));
&lt;/pre&gt;


&lt;p&gt;Somewhere inside the output, you'll find the following piece of text:&lt;/p&gt;

&lt;pre&gt;
  array(2) {
    [0]=&amp;gt;
    int(307)
    [1]=&amp;gt;
    string(4) &amp;quot;self&amp;quot;
  }
&lt;/pre&gt;


&lt;p&gt;And the token id &lt;code&gt;307&lt;/code&gt; is resolved by &lt;code&gt;token_name&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2008/07/14/About-the-self-keyword-in-static-methods#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2008/07/14/About-the-self-keyword-in-static-methods#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/1112</wfw:commentRss>
      </item>
    
</channel>
</rss>