假设我正在构建一个基于 OOP 的用户身份验证系统,我想采用以下原则:直接注入(inject)、继承、封装、多态和单一职责原则。
我的编程背景一直依赖于过程编程,因此我发现很难将这些实践真正正确使用。
假设我有这些类:
class Config
{
public function set($key, $value);
public function get($key, $default = null);
}
class User
{
public function __construct(PDO $dbh, $id = null);
public function setProfile(Profile $profile);
}
class Auth
{
public function __construct(Config $config);
public function login($username, $password, $keepLoggedIn = true);
public function isLoggedIn();
public function getLoggedInUser();
public function logout();
public function register(array $data);
}
class Session
{
public function start($sessionName = null);
public function write($key, $value);
public function read($key, $default = null);
}
class Profile
{
public function setAddress(Address $address);
public function setName($name);
public function setDOB(DateTime $date);
public function getAge();
}
class Validator
{
public function validate($input);
}
为了简单起见,我特意省略了函数体。
据我所知,我相信我正确地使用了这些原则。但是,我仍然不清楚您将如何连接类,例如:Validator
到 User
模型,User
模型到 Auth
和 Session
到 Auth
类。所有这些都相互依赖。
最佳答案
您走在正确的轨道上。这些类相互连接的方式称为扩展。我倾向于采用 MVC 设置,即模型模型、 View View 、控制 Controller 。
您的逻辑进入 Controller ,您所有的数据库查询和具体的后端方法进入模型。 Controller 接收请求并返回响应。是中间人。它在收到请求后与后端对话,并通过响应反馈给前端。
所以你有一个核心 Controller (保持最小),然后你创建的每个类都扩展了核心 Controller 。所以您的 Controller 是您将所有这些联系在一起的地方。
<?php
//your main core controller, where you load all these things you need avilable, so long as this class is extended
class CoreController {
public $auth
public $session;
public $view;
function construct__ ()
{
$this->auth = instantiateAuthClassHere();
$this->session = instantiateSessionClassHere();
$this->view = instantiateViewClassHere();
}
public function anotherHelperForSomething(){
//helper stuff for this method
}
}
//index, page, or content controller, depending on how many you need, i.e. if you want a controller for each page, thats fine, e.g indexController, etc..
//this is the middle man, has logic, receives requst, returns response to view.
class Controller extends CoreController {
public function index (){
$userModel = new userModel();
//do something with this
$session = $this->session;
$content = 'some html';
$userInfo = $userModel->getUsers();
$view = $this->view->render( array(
'content' => $content,
'userInfo' => $userInfo,
));
return $view;
}
}
//Core LIbraries
class Validator {
//your validator stuff
}
//Core LIbraries
class Session {
//your validator stuff
}
//Core LIbraries
class Auth {
//your validator stuff
}
class CoreModel{
public $validator;
function __construct(){
$this->validator = instantiateValidatorClassHere();
}
}
//a user model class (back end). you want a model class for each db table pretty much.
class UserModel extends CoreModel {
// if you need the validator anywhere inside this class, its globally available here inside any class that extends the CoreModel, e.g. $this->validator->methodName()
public function getUsers (){
$sql = 'SELECT * from users';
$result = $db->get($sql);
return $result;
}
}
请注意,在 Controller
上,这是类似 indexController
或任何自定义内容的通用名称。另外,我在那里有 extends
这个词。它继承了它扩展的父对象的所有对象。在其中,现在可以通过 $this->
访问它们。请参阅我的示例,其中我获得了 $this->session
。
尽量避免构造 - 除了核心之外,您可能在任何地方都不需要它们,在特殊情况下,您可能需要在这样做之前自行检查。我不再使用构造了。它可能有点笨重且难以管理。
关于基于PHP OOP的登录系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860233/