【问题标题】:Auto call one method in several other method in a class in PHP在 PHP 的一个类中自动调用其他几个方法中的一个方法
【发布时间】:2017-10-07 11:32:15
【问题描述】:

让我们假设这个类:

<?php

namespace app;

class SimpleClass {

    protected $url = '';
    protected $method = 'GET';

    public function __construct( $url, $method = 'GET' )
    {
        $this->url = $url;
        $this->method = $method;
    }

    public function get()
    {
        $this->prepare_something();
        // other things...
    }

    public function post()
    {
        $this->prepare_something();
        // other things...
    }

    public function patch()
    {
        $this->prepare_something();
        // other things...
    }

    public function put()
    {
        // other things...
    }

    public function delete()
    {
        // other things...
    }

    protected function prepare_something()
    {
        // preparing...
    }

正如你在这个类的三个方法中看到的; get, post, patch 我们使用 preparing_something 方法,但在 put, delete 方法中我们不使用。

我不得不重复 3 次 $this-&gt;prepare_something();。这3种方法中的一次get, post, patch。在这 3 个方法的开头是 3 lines 相同的调用。

但假设我们有 100 种方法。

在其中 70 个中我们使用 $this-&gt;prepare_something();,而在 30 个中我们没有。

这70种方法中有没有办法auto-call这些方法?无需编写这 70 种方法中的每一种$this-&gt;prepare_something();

这只是痛苦,在某些方法中必须一直调用相同的方法$this-&gt;prepare_something(); 感觉不对...

【问题讨论】:

    标签: php class oop methods


    【解决方案1】:

    使用魔术方法__call()

    • __call() - 任何时候调用不可访问的方法__call 都会被调用,因此这不适用于公共方法,您至少必须重命名为 protected。

    参考:http://php.net/manual/en/language.oop5.magic.php

    public function __call($method,$args) 
    {
            // if method exists
            if(method_exists($this, $method)) 
            {
                // if in list of methods where you wanna call
                if(in_array($method, array('get','post','patch')))
                {
                    // call
                    $this->prepare_something();
                }
    
                return call_user_func_array(array($this,$method),$args);
            }
    }
    

    请注意:这不适用于public 方法,这是结果。

    测试结果:

    akshay@db-3325:/tmp$ cat test.php 
    <?php
    
    class Test {
    
           public function __call($method,$args) 
           {
             // if method exists
             if(method_exists($this, $method)) 
             {
                // if in list of methods where you wanna call
                if(in_array($method, array('get','post','patch')))
                {
                    // call
                    $this->prepare_something();
                }
    
                return call_user_func_array(array($this,$method),$args);
              }
            }
    
        protected function prepare_something(){
            echo 'Preparing'.PHP_EOL;
        }
    
        // private works
        private function get(){
            echo 'get'.PHP_EOL;
        }
    
        // protected works
        protected function patch(){
            echo 'patch'.PHP_EOL;
        }
    
        // public doesn't work
        public function post(){
            echo 'post'.PHP_EOL;
        }
    }
    
        $instance = new test;
    
        // protected works
        $instance->prepare_something();
    
        // private works
        $instance->get();
    
        // protected works
        $instance->patch();
    
        // public does not work
        $instance->post();
    
    ?>
    

    执行:

    akshay@db-3325:/tmp$ php test.php 
    Preparing
    Preparing
    get
    Preparing
    patch
    post
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 2017-06-20
      • 2017-02-27
      相关资源
      最近更新 更多