3

有没有办法允许 enum 或 const 作为 PHP 中的方法参数。在 Qt/C++ 中,您可以像这样使用它,但当然 C++ 支持(取决于语言)。

Qt/C++/SslSocket

enum PeerVerifyMode {
    VerifyNone,
    QueryPeer,
    VerifyPeer,
    AutoVerifyPeer
};

void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode);

我 PHP 我试过这个:

第一的:

class Controller_My
{
  const MENU_FRONT   = 0;
  const MENU_SESSION = 1;

  public function render($menu_model)
  {
    $menu_model = intval($menu_model);

    if( $menu_model === 0 )
      $menu = new Model_Menu_Front();

    if( $menu_model === 1 )
      $menu = new Model_Menu_Session();
  }
}

我也读过这篇文章如何在 php 函数中使用类中的常量作为参数定义?. 但即使使用 http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism interface/implements 解决方案,如果您偏执,您也可以使用 switch/if 语句。像这样:

class EmployeeType...
  int payAmount(Employee emp) {
    switch (getTypeCode()) {
        case ENGINEER:
           return emp.getMonthlySalary();
        case SALESMAN:
           return emp.getMonthlySalary() + emp.getCommission();
        case MANAGER:
           return emp.getMonthlySalary() + emp.getBonus();
        default:
           throw new RuntimeException("Incorrect Employee");
    }
}
4

1 回答 1

2

SplEnum和用户注释为例。

于 2012-05-14T12:01:10.983 回答