Asked  7 Months ago    Answers:  5   Viewed   35 times
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,


currently i am getting output as


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.



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


class test {
    function __construct(){}

    private function test1(){
        echo "In test1", PHP_EOL;
    private function test2(){
        echo "test2", PHP_EOL;
    protected function test3(){
        return "test3" . PHP_EOL;
    public function __call($method,$arguments) {
        if(method_exists($this, $method)) {
            return call_user_func_array(array($this,$method),$arguments);

$a = new test;
echo $a->test3();
* Output:
* In test1
* test2
* In test1
* test3

Please notice that test2 and test3 are not visible in the context where they are called due to protected and private. If the methods are public the above example will fail.

test1 does not have to be declared private. example can be found here

Updated: Add link to ideone, add example with return value.

Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

What are you using to do your asynchronous call? Did you code it yourself or are you using a library like JQuery?

You could simply put a bool to say "working" that you set to true as method 1 starts and back to false when it finishes. you could then have method2 wait while working is true.

Wednesday, August 18, 2021
Bart Kiers
answered 3 Months ago

Include the script that contains the function you need using the require_once() method, like this:


Once you use the above method, PHP basically tacks the included script into the parent script, which will allow you to call functions, methods, and variables, as if they were in the parent script itself.

I would recommend require_once() over include(), include_once(), or require(), because this method will make sure that the script you are looking for exists, and is only called once. Calling the same script more than once (usually happening by accident) can cause strange problems in your script.

I hope that helps.

Sunday, August 22, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :