有没有办法允许枚举或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();
}
}
我也读过这篇文章how to use constant from class as an argument definition in php function?。但即使使用http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism接口/实现解决方案,如果您处于偏执状态,也可以使用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");
}
}