Patrik: OOP - Aufruf einer Protected Method

Hallo,

vllt. kann mir einer von Euch ja mal folgendes Verhalten erklären:

Ich habe eine Klasse (1), die ihrerseits eine andere Klasse
instanziiert (2). Nun möchte ich aus (1) heraus eine protected
Method der Klasse (2) aufrufen. Doch ich bekomme dann folgenden
Laufzeitfehler geschmissen
"Fatal error: Call to protected method SQLLib::markEntryAsRead()
from context 'ModulBrowser' [..]".

Warum dieser Fehler?
Nach eingetlicher OOP-Logik kann ich doch sehr wohl eine protected
Method von einer nicht ableitenden Klasse aufrufen.

Hier mal mein Code:

===ModulBrowser.class.php5 (1)===
public $SQLHandler; [..]
$this->SQLHandler = new SQLLib; [..]
$this->SQLHandler->markEntryAsRead($this->modul,$entryId,$usrId);

===SQLLib.class.php5 (2)===
/**
 * @access protected
 * Marks an Entry as read.
 * @param String $modulName - Name of the Modul
 * @param int $entryId - ID of the Entry
 * @param int $usrid - ID of the User
 * @return boolean - Success?
*/
protected function markEntryAsRead($modulName,$entryId,$usrId)
{
  [...]
}

Danke für Antworten.

Patrik

  1. Hallo,

    Autsch. Es ist wirklich so nicht in PHP implementiert.
    Ich kann es gar nicht glauben!

    Siehe http://de.php.net/language.oop5.visibility
    Oder hier auch ein Auszug:
    Quelle: http://www.i-marco.nl/weblog/pivot/entry.php?id=21

    A protected method can only be called from code within the original class like with private methods, but also from code in classes that extend the one the protected method was defined in:

    Class clsDemo  {
      protected function protectedMethod()  {
        return "protected method was called";
        }
      }
    Class clsExtends extends clsDemo  {
     function printOut()  {
      return $this->protectedMethod();
      }
    }

    $objDemo = new clsExtends();
    echo $objDemo->printOut();
    $objDemo2 = new clsDemo();
    echo $objDemo2->protectedMethod();

    The class will probably speak for itself. When we call the printOut() method of the extending class we will see the output without any problem. However when trying to call protectedMethod() from the outside like we are doing in the last line of this example, PHP will throw an error:

    PHP Fatal error: Call to protected method clsDemo::protectedMethod() from context ''

    Private and protected members and methods were a long awaited feature for PHP. It brings PHP a lot closer to languages like JAVA and the UML modeling language. But this isn't everything, not even by far!
    <<<

    So ein Mist aber auch.

  2. Hi,

    Nach eingetlicher OOP-Logik kann ich doch sehr wohl eine protected
    Method von einer nicht ableitenden Klasse aufrufen.

    Woher nimmst du diese Behauptung? Sämtliche mir bekannte Sprachen sagen gegenteiliges (wobei das genau so falsch wäre, wie sich an einer Datenbank zu orientieren um festzustellen, was der SQL-Standard sagt; Von der OMG finde ich aber gerade nichts). Ich habe gefunden:
    Java:
    protected: A protected field or method is accessible to the class itself, its subclasses, and classes in the same package.

    Delphi:
    Protected Defines data as internal this class and subclasses

    C#:
    Protected members are similar to private ones in that they are accessible only by the containing class. However, protected members also may be used by a descendant class. So members that are likely to be needed by a descendant class should be marked protected.

    Wenn du den Zugriff auf die Methode hättest, wo wäre denn dann der Sinn von protected?

    MfG
    Rouven

    --
    -------------------
    ss:) zu:) ls:& fo:) de:< va:{ ch:? sh:) n4:( rl:? br:$ js:| ie:) fl:(
    1. Hi,

      habe den Wald nicht gesehen. Du hast natürlich Recht.
      Bin derzeit wohl von Java verwöhnt, da es dort, zumindest
      im Packages, funktioniert. My Fault.

      thx&mfg
      Patrik