Schutzstaffel
Schutzstaffel

Reputation: 217

PHP access private methods from one child to another

class Master{
  protected static $DB;
  function __construct(){
    static::$DB = new DB();
    $view = new View();
  }
}

class DB extends Master{
  private function ReturnSomeData(){
    return $data;
  }
}

class View extends Master{
  public function ViewData(){
    $DBdata = static::$DB->ReturnSomeData();
  }
}

Fatal error: Call to private method DB::ReturnSomeData() from context 'View'

How can I access the ReturnSomeData() method from the View class? Is there something like a 'gateway'?

class Master {
... }

class DB extends Master{
...
  public function PassItToMe(){
    return $this;
  }
}

class View extends Master{
  public function ViewData(){
    $DBdata = static::$DB->PassItToMe()->ReturnSomeData();
   }
}

This is my picture right now, but I'm really lost. The idea is that I want to access private methods from one child class to another.

Upvotes: 0

Views: 83

Answers (1)

moonwave99
moonwave99

Reputation: 22817

You have to choose:

  • you want to keep ReturnSomeData() private? Fine, you won't be able to access it from outside class [not even subclasses];
  • you want to access ReturnSomeData()? Make it public.

The idea is to make private [or protected] fields, and public accessors in case, that is one of the main points of encapsulation.

Upvotes: 2

Related Questions