2017-10-07 3 views
1

Supposons que cette classe:appel automatique d'une méthode dans plusieurs autres méthodes dans une classe en PHP

<?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... 
    } 

Comme vous pouvez le voir dans trois méthodes de cette classe; get, post, patch nous utilisons la méthode preparing_something mais dans les méthodes put, delete nous ne le faisons pas.

J'ai dû répéter 3 fois $this->prepare_something();. Une fois dans ces 3 méthodes get, post, patch. C'est 3 lines le même appel au début de ces 3 méthodes.

Mais imaginez que nous avons 100 méthodes.

Dans 70 d'entre eux, nous utilisons $this->prepare_something(); et dans 30 nous ne le faisons pas.

Y a-t-il un moyen de auto-call ces méthodes dans ces 70 méthodes? Sans écrire dans chacune de ces 70 méthodes $this->prepare_something();?

Il est juste la douleur et il ne se sent pas bien d'avoir à appeler tout le temps la même méthode $this->prepare_something(); dans certaines méthodes ...

Répondre

2

Utiliser des méthodes magiques __call()

  • __call() - Chaque fois qu'une méthode inaccessible est appelé __call sera appelé, donc cela ne fonctionne pas avec les méthodes publiques, vous devrez renommer au moins protégé.

Ref: 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); 
     } 
} 

S'il vous plaît noter: Cela ne fonctionne pas avec la méthode public, voici les résultats pour cela.

Résultats des tests:

[email protected]:/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(); 

?> 

Exécution:

[email protected]:/tmp$ php test.php 
Preparing 
Preparing 
get 
Preparing 
patch 
post