TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   OOP: prevent method X in context Y (http://www.talkphp.com/advanced-php-programming/5400-oop-prevent-method-x-context-y.html)

sunwukung 04-09-2010 10:05 AM

OOP: prevent method X in context Y
 
I wanted to know if there was a way to prevent a method in a class being called unless it is called by a specific (non-inheritance chain) object. I'm borrowing from the Zend Bootstrapping pattern:

Code:

class abstract Config_Abstract(){
    public function runConfig(){
        //iterate over methods declared in child and execute each in turn
  }
}

class Config extends Config_Abstract(){
  publc function initGood(){
      //do some stuff
  }

  publc function initBad(){
      $this->runConfig(){//VERY BAD - CIRCULAR CODE
      }
  }
}

class Application(){
    public function run(){
        $Config = new Config();
        $Config->runConfig();//should only be executed by Application class
    }
}

$App = new App();
$App->run();

there are two methods I've thought of so far, but I wanted to know if Reflection or magic methods had something that could deal with this

1: use debug_backtrace() to detect the calling object
2: pass in a reference to the calling object in the configRun() argument

Can anyone offer some insight?

sunwukung 04-09-2010 01:21 PM

I've just figured it out. The calling class (Application) should be given responsibility for executing runConfig - not the Config class...DOH!
I knew something was smelling...


All times are GMT. The time now is 12:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0