<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MoMolog &#187; PHP</title>
	<atom:link href="http://momolog.info/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://momolog.info</link>
	<description>MoMolog aus Berlin stellt sich vor. Projekte, Ideen, Referenzen.</description>
	<lastBuildDate>Sun, 18 Jul 2010 22:13:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>What ist this?</title>
		<link>http://momolog.info/2009/05/15/what-ist-this/</link>
		<comments>http://momolog.info/2009/05/15/what-ist-this/#comments</comments>
		<pubDate>Fri, 15 May 2009 17:25:24 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://momolog.info/?p=37</guid>
		<description><![CDATA[Laut PHP-Doku ist das Verhalten der Pseudo-Variablen $this wie folgt: $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object). Schauen wir uns folgendes Beispiel an: class A{ &#160;&#160;function __construct(){ &#160;&#160;&#160;&#160;$this-&#62;name [...]]]></description>
			<content:encoded><![CDATA[<p>Laut <a href="http://www.php.net/manual/en/language.oop5.basic.php">PHP-Doku</a> ist das Verhalten der Pseudo-Variablen <code>$this</code> wie folgt:</p>
<blockquote><p>$this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).
</p></blockquote>
<p>Schauen wir uns folgendes Beispiel an:<br />
<pre><pre>
class A{
&nbsp;&nbsp;function __construct(){
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name = &#039;A&#039;;
&nbsp;&nbsp;}

&nbsp;&nbsp;function echoThisName(){
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;My Name is {$this-&gt;name}.\n&quot;;
&nbsp;&nbsp;}
}
</pre></pre><br />
Jetzt rufen wir die Methode mal als Instanzmethode und mal statisch auf:<br />
<pre><pre>
$a = new A();
$a-&gt;echoThisName();
A::echoThisName();

My Name is A.
PHP Fatal error:&nbsp;&nbsp;Using $this when not in object context in 
/Users/aljoscha/test.php on line 9
Fatal error: Using $this when not in object context in 
/Users/aljoscha/test.php on line 9
</pre></pre><br />
Das ist vernünftig. (ausser: Warum muss sich PHP eigentlich immer wiederholen? Ist eine Fehlermeldung zu subtil?)<br />
Jetzt rufen wird diese Methoden aus einer anderen Klasse <code>B</code> heraus auf:<br />
<pre><pre>
class B{
&nbsp;&nbsp;function __construct(){
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name = &#039;B&#039;;
&nbsp;&nbsp;}

&nbsp;&nbsp;function echoAsName(){
&nbsp;&nbsp;&nbsp;&nbsp;$a = new A();
&nbsp;&nbsp;&nbsp;&nbsp;$a-&gt;echoThisName();
&nbsp;&nbsp;&nbsp;&nbsp;A::echoThisName();
&nbsp;&nbsp;}
}

$b = new B();
$b-&gt;echoAsName();
</pre></pre><br />
Und das gibt:<br />
<pre><pre>
My Name is A.
My Name is B.
</pre></pre><br />
Wir haben die Methode <code>echoThisName</code> der Klasse <code>A</code> <em>statisch</em> aufgerufen, aber <code>$this</code> ist darin trotzdem gesetzt, und zwar als wären wir in der Instanz <code>$b</code> der Klasse <code>B</code>.<br />
<code>$b</code> hat sich die statische Methode gekapert. </p>
<p>Was sagt Ruby dazu?<br />
<pre><pre>

class A
&nbsp;&nbsp;def initialize
&nbsp;&nbsp;&nbsp;&nbsp;@name = &#039;A&#039;;
&nbsp;&nbsp;end

&nbsp;&nbsp;def echoThisName
&nbsp;&nbsp;&nbsp;&nbsp;puts &quot;My Name is #{@name}.\n&quot;;
&nbsp;&nbsp;end
end

$a = A.new;
$a.echoThisName;
A::echoThisName;
</pre></pre><br />
ergibt:<br />
<pre><pre>

My Name is A.
test.rb:13: undefined method `echoThisName&#039; for A:Class (NoMethodError)
</pre></pre><br />
Rufen wir <code>A::echoThisName</code> aus einer Instanz von <code>B</code> auf:<br />
<pre><pre>

class B
&nbsp;&nbsp;def initialize
&nbsp;&nbsp;&nbsp;&nbsp;@name = &#039;B&#039;;
&nbsp;&nbsp;end

&nbsp;&nbsp;def echoAsName
&nbsp;&nbsp;&nbsp;&nbsp;$a = A.new;
&nbsp;&nbsp;&nbsp;&nbsp;$a.echoThisName;
&nbsp;&nbsp;&nbsp;&nbsp;A::echoThisName;
&nbsp;&nbsp;end
end

$b = B.new;
$b.echoAsName;
</pre></pre><br />
Ist das Ergebnis entsprechend:<br />
<pre><pre>
My Name is A.
test.rb:23:in `echoAsName&#039;: undefined method `echoThisName&#039; 
for A:Class (NoMethodError) from test.rb:28
</pre></pre><br />
Besser.<br />
<i>Dank an Stephan für den Hinweis auf die Dokumentation des Verhaltens in PHP.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2009/05/15/what-ist-this/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;Zen&#8221; oder &#8220;Die vier Arten von Nichts&#8221;</title>
		<link>http://momolog.info/2009/01/15/isset-und-der-wert-null/</link>
		<comments>http://momolog.info/2009/01/15/isset-und-der-wert-null/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 12:27:54 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://momolog.info/?p=11</guid>
		<description><![CDATA[Manche Dinge in PHP sind so komisch, dass man sie sich am besten einmal notiert, um sich dann in Ruhe darüber zu wundern. Falls z.B. in PHP eine Variable $a auf den Wert null gesetzt ist, liefert isset($a) trotzdem false. (Das ist nichts Neues, aber es ist merkwürdig im Wortsinne.) Interessant auch das Verhalten von [...]]]></description>
			<content:encoded><![CDATA[<p>Manche Dinge in PHP sind so komisch, dass man sie sich am besten einmal notiert, um sich dann in Ruhe darüber zu wundern.</p>
<p>Falls z.B. in PHP eine Variable <code>$a</code> auf den Wert null gesetzt ist, liefert <code>isset($a)</code> trotzdem false. (Das ist nichts Neues, aber es ist <em>merkwürdig</em> im Wortsinne.) Interessant auch das Verhalten von <code>empty</code> und <code>is_null</code> für ungesetzte Variablen und Variablen mit dem Wert <code>null</code>. Dazu initialisieren wir Variable <code>$a</code> mit dem Wert <code>null</code> und lassen Variable <code>$b</code> völlig uninitialisiert:<br />
<pre><pre>
$a = null;
# $b wird nicht initialisiert
</pre></pre><br />
Hier nun die Tests:</p>
<table>
<tr>
<th>Test</th>
<th class="code">$a</th>
<th class="code">$b</th>
</tr>
<tr>
<td class="code">isset()</td>
<td class="code">false</td>
<td class="code">false</td>
</tr>
<tr>
<td class="code">empty()</td>
<td class="code">true</td>
<td class="code">true</td>
</tr>
<tr>
<td class="code">is_null()</td>
<td class="code">true</td>
<td class="code">true</td>
</tr>
<tr>
<td class="code">=== null</td>
<td class="code">true</td>
<td class="code">true</td>
</tr>
</table>
<p>Das Ergebnis von <code>isset($a)</code> ist zumindest gegen jede Intuition. </p>
<p>Bizarr wird es aber bei der gedoppelten (?) Notice <pre><code>PHP Notice: Undefined variable: b in /home/aljoscha/test.php on line 10
Notice: Undefined variable: b in /home/aljoscha/test.php on line 10</code></pre>, die durch <code>is_null($b)</code> bzw. <code>($b === null)</code> ausgelöst wird, und dem überraschenden Ergebnis: <code>true</code>! </p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2009/01/15/isset-und-der-wert-null/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
