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,

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.

 Answers

14

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

<?php

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)) {
            $this->test1();
            return call_user_func_array(array($this,$method),$arguments);
        }
    }
}

$a = new test;
$a->test2();
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.

ideone.com example can be found here

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

Wednesday, March 31, 2021
 
felipsmartins
answered 7 Months ago
86

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: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

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

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

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
 
Nate
answered 5 Months ago
53

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
31

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

require_once("../path/to/script.php");

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
 
PeterTheLobster
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 :