Viewed   59 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

4

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, September 21, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
5

You can call it something like this:

function one() {
    echo '
        <div id="two-post">
            <a href="' . the_permalink() .'" alt="' . the_title() .'" title="' . the_title() .'">
                ' . the_post_thumbnail('dos') . '

                <div class="entry-meta">
                    <h1>' . the_title() . '</h1>
                    <p>By ' . the_author() . '</p>
                </div>

                <div class="overlay2"></div>
            </a>
        </div>
    ';
}

function two() {
    echo wp_trim_words( get_the_content(), 50, '' );
}

function three() { // function names without "-"
    echo '<div>' . the_author() .'</div>';
}

$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings

$functions[array_rand($functions)](); // call it!

// or
call_user_func($functions[array_rand($functions)]);
Saturday, August 13, 2022
4

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.

Tuesday, September 13, 2022
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :