当我调用类的成员函数时如何自动调用函数?

In php is there any possible way to call an new function automatically while calling a member function of the class

For Example : I have writing a class with 4 member functions . And then I have created the object for that class. Now I am going to call any one of the function as I needed . When I call an any one of the function of that class . I needed to do some set/Logic , how can I do this

Note: I am not willing to call an new function inside the defined functions and also not need to write a logic for all defined functions . I am looking for any magic methods . Please advice me

Class IMAP{
Function IMAP()
{ 
Do something 
}
Function getfolders() {
Do something
}
Function appendmessage()
{
Do something
}
//I need to call the below function     whenever I am going to call any one of the function 
Function checktokenexpired()
{
}
}

This class contains lot functions I am not possible to add this function in all functions

You should look at PHP magic function __call which allows you to implement method overloading.

If you don't want a full-blown AOP library, you can start with a small wrapper like this:

class AOP
{
    function __construct($base, $methods) {
        $this->base = $base;
        $this->methods = $methods;
    }

    function __call($name, $args) {
        $this->methods["before_$name"]($args);
        $ret = call_user_func_array([$this->base, $name], $args);
        $this->methods["after_$name"]($ret);
        return $ret;
    }
}

Usage like this:

class Foo
{
    function bar() {
        echo "bar 
";
    }
}

$foo = new AOP(new Foo, [
    'before_bar' => function() { echo "BEFORE
"; },
    'after_bar'  => function() { echo "AFTER
"; },
]);

$foo->bar(); // prints BEFORE...bar...AFTER

While what i'm writing here is not an answer per say, You must be exteremely careful when using __CALL as suggested here. The main reason is that you lose all control over visibility of functions, all functions are accessible which may or may not be what you want.

Other than __CALL though, What you want is called a proxy wrapper, Check out the answer by ocramius in this thread :

How to auto call function in php for every other function call

Notice that __CALL should always be avoided, if __CALL is the answer, the question is normally wrong.