Jump to content

OOP: prevent method X in context Y


sunwukung

Recommended Posts

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:

 

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?

Link to comment
Share on other sites

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

 

No. What are you actually trying to accomplish quite possible there is just a problem with your app design and we can correct it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.