tags:

views:

235

answers:

5

New to OOP in PHP

One of my functions requires another function to be executed before running. Is there a way I can check this?

+7  A: 

Language-agnostic answer:

Keep a (static or global) "state" variable and set a flag within the prerequisite function when it's called. Check the flag in the dependent function to decide whether it's allowed to run.

Adam Liss
Generally, a global state variable would probably be inappropriate. An object member variable would be better.
troelskn
@troelskn: Yes, definitely. Good call!
Adam Liss
+4  A: 

The other function (the one, which should be called first) will manipulate some state, thats why the second function needs to know about that state. I don't know about a php builtin for that but I would eventually create another function, which returns information about that state, e.g. isReadyToRunSomething, isValid, hasConnection .. or whatever .. then use this function in the beginning of the second function, to see whether it is allowed to run.

The MYYN
Why not do the check before you call the function...?
Andrew Song
A great suggestion, this way you can later effortlessly change the way you determine if the function can be run or not.
Igor Zinov'yev
@Andrew: because the prerequisites may change, and the function may be called from several places. That would increase the likelihood of introducing errors during maintenance. Checking the requirements within the function itself places all the logic in a single, sensible place and minimizes both the maintenance effort and the risk of introducing bugs. The minimal overhead is a small price to pay for this.
Adam Liss
+4  A: 

Well, the easiest solution would be to simply call this method before you run the method that needs it. If you do not want to run the method each time, but only when some internal state of your object applies, you'd do

class Foo
{
    protected $_someState = 'originalState';

    public function runMeFirst()
    {
        // code ...
        $this->_someState = 'changedState';
    }

    public function someMethod()
    {
        if(!$this->_someState === 'changedState') {
            $this->runMeFirst();
        }
        // other code ...
    }
}

As long as the method and state that needs to be checked and called are inside the same class as the method you want to call, the above is probably the best solution. Like suggested elsewhere, you could make the check for someState into a separate function in the class, but it's not absolutely necessary. I'd only do it, if I had to check the state from multiple locations to prevent code duplication, e.g. having to write the same if statement over and over again.

If the method call is dependent on state of an outside object, you have several options. Please tell us more about the scenario in that case, as it somewhat depends on the usecase.

Gordon
A: 

Create a third function C that calls A and then B, and use that new function all over the place. In OOP terms, A and B should be protected in your class and C is public, e.g. available for whoever uses your class.

stereofrog
I think the question is, "How can I prevent a function from running unless another function has already been run?" rather than "How can I always run a particular function before calling a second one?"
Adam Liss
the first statement sounds like a (wrong) answer to me rather than a question. There is no such thing as a function that "isn't allowed to run". If you don't want something to be called, it shouldn't exist in the context.
stereofrog
+1  A: 

The observer pattern can be used to do what you want (notify observers the event is going to happen so they can do something).

koen