我正在使用Front Controller设计模式构建应用程序,并且只有一个页面index.php,所有用户请求都通过该页面作为参数传递(与常规设计中的不同页面/控制器相对).
如何将这些参数连接到应用程序逻辑?
例如,我有两种不同的行为:
index.php?action=userLogin&username=admin&password=qwerty //process user login
index.php?action=displayUsersTable //show registered users
Run Code Online (Sandbox Code Playgroud)
目前我有一个array系统接受的所有操作(以及预期的参数),我将actionURL中的param与key此数组的比较进行比较,然后检查此操作所需的参数.
//1 = optional, 2=required
$systemActions = [
"userLogin" => [
"login" => 2,
"password" => 2
],
"displayUsersTable" => []
];
Run Code Online (Sandbox Code Playgroud)
显然,随着系统的发展,这将成为一个怪物阵列.
是否有更好的方法将发送到前端控制器的参数绑定到系统操作?
子类是否无法实现相同的接口父类实现的正常行为?我得到了PHP v5.6
interface blueprint {
public function implement_me();
}
class one implements blueprint {
public function implement_me() {
}
}
class two extends one implements blueprint {
}
//no fatal error triggered for class two
Run Code Online (Sandbox Code Playgroud)
编辑:所以上面的代码工作很好没有错误或警告,即使我blueprint在子类中实现接口two没有方法impement_me()为什么子类不能实现相同的接口父类实现?
如果我实现除了blueprint类之外的其他接口two然后它工作,我必须使用blueprint_new类内部的方法,two否则触发致命错误.这部分按预期工作.
interface blueprint {
public function implement_me();
}
class one implements blueprint {
public function implement_me() {
}
}
interface blueprint_new {
public function todo();
}
class two extends …Run Code Online (Sandbox Code Playgroud) 尝试使用PHP 实现方法重载(不要与PHP手册中的重载定义混淆),要进行明确的权衡是不容易的,因为PHP的性质,您必须在内部提供一种方法和/ if或switch语句来提供重载,它会创建在长方法中难以阅读的过程代码。
在PHP中有方法重载是否有任何优势,可以通过其他任何方式实现吗?
class MyClass {
public function doMany($input) {
if (is_array($input)) {
...
} else if (is_float($input)) {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
与传统方法
class MyClass {
public function doArray(array $input) {
...
}
public function doFloat(float $input) {
...
}
}
Run Code Online (Sandbox Code Playgroud)