3

I want to call a function every time a method call is made in PHP.

Example :

$a = new a();
$a->function1();

this would make 2 function calls: basicFunction() and then function1()

Same would be the case for any subsequent method calls of that class. is this possible in object oriented PHP?

1
  • It's not clear, whether you want to have basicFunction called when function1 is called or when the ctor is called or when any function is called. Also, is basicFunction part of the the a class? Please clarify the question and maybe add a concrete example illustrating what you need this for. Commented Dec 13, 2010 at 9:17

5 Answers 5

6

The simplest would be to just call the method

public function function1 {
    $this->basicFunction();
    // …
}

What you are probably after is something like Aspect Oriented Programming. However, this is not common in PHP. The most notable use is in the Flow3 framework.

For other more common approaches usable with PHP OOP, have a look at

These would allow you to add arbitrary behavior and control flow at runtime.


(meta) On a sidenote, do we really need four answers suggesting to use __call? I suggest the various OPs review their respective answers, sort out the tiny differences and agree on which one is best. The other three could withdraw their answers then.

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

Comments

3

There is nothing like this in PHP. You can, however, simulate this behaviour using the magic method __call():

class YourClass {
    public function __call($method, $args){
        // do what you want to do everywhere (i.e. call your basicFunction)
        return call_user_func_array(array($this, 'base'.ucfirst($method)), $args);
    }

    private baseSomeFunction() {
        // ...
    }
}

Usage:

$obj = new YourClass();
$obj->someFunction(); // this calls baseSomeFunction and executes the code
                      // in the __call() method

Prefixing your "magic" methods with something like base ensures that nobody can call your private functions and limits this access to only the functions that have been prefixed. You should change this prefix to suit your needs.

Comments

3

Sure, although it involves some overhead. The magic method _call will handle non-existing methods so you could do:

class myClass {
    public function __call($name, $arguments) {

        $this->doSomething();
        $this->$name($arguments);
    }

    private function calledmethod() { ... }

And then you'd call:

$class->calledmethod();

Since the method calledmethod isn't public, _call would get invoked then would run your 'base' method and then calledmethod.

I do feel obliged to ask, WHY do you need to do this? There's probably a more elegant solution if you'd describe the actual problem you're trying to solve.

7 Comments

I think - in function names in invalid.
This will not return the function output.
@elusive: fixed, too much js lately, thanks. @cypher: OP never said anything about returning a value.
I'd say that returning a value of the original called function is pretty much implied as he didn't say he wishes that no method of his function will return a value.
@Erik: - is allowed in variable names in JavaScript? Didn't know that. I know that you could use every string like this: window['some-function-name']() but that would be kind of a pain, right?
|
0

Use __call magic function like this:

class a {
  private function function1() { ... }
  public function __call($name, $args) {
       /* do stuff */;
       return call_user_func_array(array($this, $name), $args);
  }
}

Note however this won't work if function1() is called from inside the class. Then you'd have to rename function1() and call it with different name. The idea is that __call is invoked only if the function doesn't exist or can not be seen.

2 Comments

I could call every private function of this class.
@elusive - if you're bothered by this, put some conditions into __call. It's just a suggestion, not production code :)
0

you have to use the __call magic method For your example:

class Foo{
    public function __call($method_name, $arguments){
        $this->basicFunction();
        return call_user_func_array(array($this, $method_name), $arguments);
    }


    private function basicFunction(){}
    protected function function1(){}
}

2 Comments

if function1() is public, __call won't be used.
I could call every private method now.

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.