inicio mail me! sindicaci;ón

What ist this?

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 = 'A';
  }

  function echoThisName(){
    echo "My Name is {$this->name}.\n";
  }
}

Jetzt rufen wir die Methode mal als Instanzmethode und mal statisch auf:

$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

Das ist vernünftig. (ausser: Warum muss sich PHP eigentlich immer wiederholen? Ist eine Fehlermeldung zu subtil?)
Jetzt rufen wird diese Methoden aus einer anderen Klasse B heraus auf:

class B{
  function __construct(){
    $this->name = 'B';
  }

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

$b = new B();
$b->echoAsName();

Und das gibt:

My Name is A.
My Name is B.

Wir haben die Methode echoThisName der Klasse A statisch aufgerufen, aber $this ist darin trotzdem gesetzt, und zwar als wären wir in der Instanz $b der Klasse B.
$b hat sich die statische Methode gekapert.

Was sagt Ruby dazu?


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

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

$a = A.new;
$a.echoThisName;
A::echoThisName;

ergibt:


My Name is A.
test.rb:13: undefined method `echoThisName' for A:Class (NoMethodError)

Rufen wir A::echoThisName aus einer Instanz von B auf:


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

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

$b = B.new;
$b.echoAsName;

Ist das Ergebnis entsprechend:

My Name is A.
test.rb:23:in `echoAsName': undefined method `echoThisName' 
for A:Class (NoMethodError) from test.rb:28

Besser.
Dank an Stephan für den Hinweis auf die Dokumentation des Verhaltens in PHP.

bkw said,

Juni 18, 2009 @ 11:10 pm

Wenigstens gibt es jetzt inzwischen den Errorlevel E_STRICT (der NICHT in E_ALL enthalten ist – WTF?!).
Das gibt dann immerhin eine Warnung:

Strict standards: Non-static method A::echoThisName() should not be called statically, assuming $this from incompatible context in /Users/bkw/test.php on line 24

Andersherum bewahrt das keyword static davor, statische Klassen als Objekt-Methoden aufzurufen.

RSS feed for comments on this post · TrackBack URI

Schreibe einen Kommentar