<?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>Sat, 21 Jan 2012 12:35:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</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{ function __construct(){ $this->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:</p>
<pre>
class A{
  function __construct(){
    $this->name = 'A';
  }

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

My Name is A.
PHP Fatal error:  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>
<p>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:</p>
<pre>
class B{
  function __construct(){
    $this->name = 'B';
  }

  function echoAsName(){
    $a = new A();
    $a->echoThisName();
    A::echoThisName();
  }
}

$b = new B();
$b->echoAsName();
</pre>
<p>Und das gibt:</p>
<pre>
My Name is A.
My Name is B.
</pre>
<p>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?</p>
<pre>

class A
  def initialize
    @name = 'A';
  end

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

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

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

class B
  def initialize
    @name = 'B';
  end

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

$b = B.new;
$b.echoAsName;
</pre>
<p>Ist das Ergebnis entsprechend:</p>
<pre>
My Name is A.
test.rb:23:in `echoAsName': undefined method `echoThisName'
for A:Class (NoMethodError) from test.rb:28
</pre>
<p>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:</p>
<pre>
$a = null;
# $b wird nicht initialisiert
</pre>
<p>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 <code>PHP Notice: Undefined variable: b in /home/aljoscha/test.php on line 10<br />
Notice: Undefined variable: b in /home/aljoscha/test.php on line 10</code>, 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>

