Heim > Backend-Entwicklung > PHP-Tutorial > 如何更优雅的写这段代码

如何更优雅的写这段代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-06 20:39:03
Original
1062 Leute haben es durchsucht

<code><?php $command = (int)$_GET['command'];

$actions = array(
    1 => 'profile',
    3 => 'login',
    7 => 'show',
    9 => 'update',
    11 => 'stop',
    13 => 'start',
    15 => 'remove',
);

//判断命令对应的动作是否存在
if (!array_key_exists($command, $actions)) throw new Exception('404');

$control = new App();
$method = 'on' . ucfirst($actions[$command]);

//判断类里面是否存在该函数
if (!method_exists($control, $method)) throw new Exception('404');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

回复内容:

<code><?php $command = (int)$_GET['command'];

$actions = array(
    1 => 'profile',
    3 => 'login',
    7 => 'show',
    9 => 'update',
    11 => 'stop',
    13 => 'start',
    15 => 'remove',
);

//判断命令对应的动作是否存在
if (!array_key_exists($command, $actions)) throw new Exception('404');

$control = new App();
$method = 'on' . ucfirst($actions[$command]);

//判断类里面是否存在该函数
if (!method_exists($control, $method)) throw new Exception('404');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

凭感觉猜测题主是需要一个简洁的分发,那么可以考虑

<code>php</code><code>class App {
  protected static $actions = [
    1 => 'onProfile',
    2 => 'onLogin',
    //...
  ];

  public function run($command) {
    if (!isset(self::$actions[$command])) { throw ...; }
    $callback = [$this, self::$actions[$command]];
    if (!is_callable($callback)) { throw ...; }

    call_user_func($callable);
  }
}


//index.php

new App()->run($_GET['command']);
</code>
Nach dem Login kopieren

先指出一点错误, 一般检测类似controller这种类方法是否可以被调用, 需要使用is_callable而不是method_exists, 前者检查方法是否可以被调用(存在且公开), 后者只是单纯检查方法是否存在。

<code>class NotFoundException extends Exception {}

$command = $_GET['command'] ?: false;

$actions = array(
    'profile',
    'login',
    'show',
    'update',
    'stop',
    'start',
    'remove',
);

//判断命令对应的动作是否存在
if ( ! in_array($command, $actions)) 
    throw new NotFoundException();

$control = new App();
$method = 'on' . ucfirst($command);

//判断类里面是否存在该函数
if ( ! is_callable(array($control, $method)))
    throw new NotFoundException();
</code>
Nach dem Login kopieren

看看 Flight 框架 也是另外一种思路

Verwandte Etiketten:
php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
PHP-Datenerfassung?
Aus 1970-01-01 08:00:00
0
0
0
PHP-Erweiterung intl
Aus 1970-01-01 08:00:00
0
0
0
Wie man PHP gut lernt
Aus 1970-01-01 08:00:00
0
0
0
Mehrere PHP-Versionen
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage