[英]How to overload child class methods in PHP, respecting OOP and SOLID principles, when method signature must change?
[英]Class methods and SOLID principles in PHP
假設我們有一個名為 Register 的類,該類僅注冊用戶並在驗證未通過時顯示一條消息。
class Register {
function register_user() {
// Do Validation, Get message and Show it if faild
...
$message = $this->get_message( $error_code );
...
// Register User Codes
...
}
// Some other related methods
...
function get_message( $error_code ) {
// Returns **different messages** based on error code.
}
}
我的問題是: get message 應該是 Register 類的一部分,還是應該有自己的類來處理 Register、Login、ForgotPassword... 的消息?
哪個更 SOLID 友好?
在一個好的 OOP 實現中,您將創建一個具有函數(方法)的用戶類,如注冊、登錄、注銷、顯示用戶、修改用戶和更多功能。 你的對象是用戶。 有一個只有 register_user 函數的類,另一個只有 login_user 函數的類,會無緣無故地使您的代碼復雜化,並且違背基本的 OOP。 我對 SOLID 了解不多,但您當然可以通過使用諸如 private、protected、final 等修飾符來使單個函數安全和健壯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.