0
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.

1
  • 1
    If you would add a "gateway" method that would allow you to access private methods based on for example a parameter you pass, that would kind of defeat the purpose of private methods and you would be better off declaring them public as that is what they will be. Commented Sep 27, 2012 at 0:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.