1

This is the basic class design

class CustomModule {

    public __construct() {  }

    //Run me first automaticly  
    public function exec($str) {  } 

}

class Randomizer extends CustomModule {

    public __construct() {  }

    //Call me
    public function exec($str) {  } 

}

As I am designing a plugin/module system for extern developers I need the CustomModule->exec() to run first, I do not want to leave it up to the devs to have to call base->exec($str).

I want CustomModule::exec() called automaticly before Randomizer::exec() is called without having to put code in Randomizer::exec(). Is This Possible perhaps with magic function?

3 Answers 3

3

In my opinion, i would use this way: Instead of calling _construct in exec of Randomizer, you can define a constructor in Randomizer and call parent::_construct

class CustomModule {

//Run me first automaticly  
public function exec($str) {  } 

public __construct($str) {
   $this->exec($str);
}

}

class Randomizer extends CustomModule {

//Call me
public function exec($str) {  
   parent::__construct($str);
} 

}

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

1 Comment

"Instead of calling _construct in exec of Randomizer.." I didnt code in a __construct() for the sample. But thanks for the tip. Ill fix my sample. cheers
2

If your object requires some initialization before you can "release* it into the rest of application, then it means that you need a factory for this. This is how you should be solving it, if you require to call some method only once.

If such execution happens each time you call exec() method, then instead you should be using some sort of containment (in a form of decorator or just you standard composition of objects).

Basically, you need to restructure your code.

1 Comment

Yes I know, as per my last post. Was messy and a bit crazy. A sign of needing some sleep. Thankyou
2

After a bit more thought I realized this is BAD design. I have to keep the code base simple and scaleable and this would only make a mess in large projects.

As the only program calling the Randomizer::exec() is my inhouse built program I can just call CustomModule::exec() on the previous line and get a boolean response to see if it should continue to the next line.

Sorry to have to end this Q' short

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.