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

前端 未结 8 1931
夕颜
夕颜 2020-11-29 02:48
Class test{
    function test1()
    {
        echo \'inside test1\';
    }

    function test2()
    {
        echo \'test2\';
    }

    function test3()
    {
            


        
相关标签:
8条回答
  • 2020-11-29 03:24
    <?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...

    0 讨论(0)
  • 2020-11-29 03:24

    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

    0 讨论(0)
提交回复
热议问题