8

Is there a magic method that when a certain method is called from an object, that a magic method is called first. Kinda like the __call method, but this only gets triggered when the method isn't found.

So in my case i'd like something like this:

class MyClass
{
    public function __startMethod ( $method, $args )
    {
        // a method just got called, so  this is called first
        echo ' [start] ';
    }

    public function helloWorld ( )
    {
        echo ' [Hello] ';
    }
}

$obj = new MyClass();
$obj->helloWorld();

//Output:
[start] [Hello] 

Does something like this exist in PHP??

4
  • 1
    There is only __construct it will be called when this part of the code will get executed: $obj = new MyClass(); Commented Apr 6, 2012 at 18:22
  • @micha, see my comment at Stony --- (Comment is removed). I'm not looking for the __construct() method. This is only called when the object is created. I need a magic method which gets called everytime a function is called. Commented Apr 6, 2012 at 18:26
  • 1
    No, there isn't. Declare your existing methods with another scheme, or use a wrapper object. Commented Apr 6, 2012 at 18:28
  • @w00 sure I know.. I've written There is only. To answer your question there isn't, you can only prepend a function to the beginning of each function in the class. -.- mario was 40 sec faster :D Commented Apr 6, 2012 at 18:29

3 Answers 3

9

There isn't a direct way to do this but it looks to me like your trying to implement a form of Aspect Oriented programming. There are several ways of achieving this in PHP, one would be to set up your class something like the following:

class MyClass
{
    public function __startMethod ( $method, $args )
    {
        // a method just got called, so  this is called first
        echo ' [start] ';
    }

    public function _helloWorld ( )
    {
        echo ' [Hello] ';
    }

    public function __call($method, $args)
    {
        _startMethod($method, $args);
        $actualMethod = '_'.$method;
        call_user_func_array(array($this, $actualMethod), $args);
    }
}

$obj = new MyClass();
$obj->helloWorld();

Look up other ways of implementing AOP in PHP to see what works best for you (I'll see if I can find a link somewhere).

EDIT: here's a document for you http://www.liacs.nl/assets/Bachelorscripties/07-MFAPouw.pdf

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

1 Comment

You can then set __startMethod and _helloWorld as private or protected, so nobody "hacks" it.
4

YOu could achieve it by making your methods private and calling the methods using the __call() magic method. Like:

<?php

class MyClass{
    function __call($mthd, $args){
        if(method_exists($this, $mthd)){
            $this->$mthd($args);
        }
    }

    private function mthdRequired($args){
        //do something or return something
    }

The mthdRequired method doesnt get called except using call. I hope this is usefull.

Comments

3

No there is no magic method for this.

The best you could do is create other names for your functions (eg: hidden_helloWorld), then catch all the calls with __call and try to call the hidden_ method if it is available. Of course this is only possible if you have full control over the naming of the class and its parent, etc...

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.