tags:

views:

116

answers:

6
Class test{
function test1()
{
echo 'inside test1';
}

function test2()
{
echo 'test2';
}

function test3()
{
echo 'test3';
}
}

$obj = new test;
$obj->test2();//prints test2
$obj->test3();//prints test3

Now my question is,

How can i call another function before any called function execution? In above case, how can i auto call 'test1' function for every another function call, so that i can get the output as,

test1
test2
test1
test3

currently i am getting output as

test2
test3

I cannot call 'test1' function in every function definition as there may be many functions. I need a way to auto call a function before calling any function of a class.

Any alternative way would also be do.

+3  A: 
<?php

class test
{

    public function __call($name, $arguments)
    {
        $this->test1(); // Call from here
        return call_user_func_array(array($this, $name), $arguments);
    }

    // methods here...

}

?>

Try adding this method overriding in the class...

Otar
thanks for suggestion but I cannot call 'test1' function in every function definition as there may be many functions. I need a way to auto call a function before calling any function of a class.
Yogesh
+1 as this is the best way to do it without violating OOP principles by specifying public methods as private although they are still accessible as public.
poke
@Yogesh, see the updated answer please...
Otar
A: 

The only way to do this is using the magic __call. You need to make all methods private so they are not accessable from the outside. Then define the __call method to handle the method calls. In __call you then can execute whatever function you want before calling the function that was intentionally called.

halfdan
+6  A: 

Your best bet is the magic method __call, see below for example:

<?php

class test
{
function __construct()
{

}

function test1()
{
    echo "In test1";
}

function _test2()
{
    echo "test2";
}

function _test3()
{
    echo "test3";
}

function __call($method,$arguments)
{
    $this->test1();
    call_user_func_array(array($this,"_".$method),$arguments);
}
}

$a = new test;
$a->test2();
Kristoffer S Hansen
+1  A: 

This should do it for you. Make sure the methods cannot be called from anywhere else by putting access modifiers on them.

class Example
{
    public function __call($name, $args)
    {
        if($name != 'call_first' && method_exists($this, $name))
        {
            $this->call_first();
            return call_user_func_array(array($this, $name), $args);
        }
    }
    private function call_first()
    {
        echo "First\n";
    }
    private function f2()
    {
        echo "F2\n";
    }
    private function f3()
    {
        echo "F3\n";
    }
}

$obj = new Example();
$obj->f2();
$obj->f3();
Tim Cooper
A: 

Lets have a go at this one :

class test
{
    function __construct()
    {

    }

    private function test1()
    {
        echo "In test1";
    }

    private function test2()
    {
        echo "test2";
    }

    private function test3()
    {
        echo "test3";
    }

    function CallMethodsAfterOne($methods = array())
    {
        //Calls the private method internally
        foreach($methods as $method => $arguments)
        {
            $this->test1();
            $arguments = $arguments ? $arguments : array(); //Check
            call_user_func_array(array($this,$method),$arguments);
        }
    }
}

$test = new test;
$test->CallMethodsAfterOne('test2','test3','test4' => array('first_param'));

Thats what I would do

RobertPitt
A: 

If you are really, really brave, you can make it with runkit extension. (http://www.php.net/manual/en/book.runkit.php). You can play with runkit_method_redefine (you can need Reflection also to retrieve method definition) or maybe combination runkit_method_rename (old function) / runkit_method_add (new function which wraps calls to your test1 function and an old function )

ts