php "pure" OOP:如何设计类?
php "pure" OOP: how design classes?
序言:我使用 F3 框架 (www.fatfreeframework.com)。
假设我有一个摘要 class "Controller"
<?php
abstract class Controller
{
/* F3 */
protected $f3;
/* DATABASE SECTION */
protected $db_name;
protected $db_host;
protected $db_username;
protected $db_password;
function __construct()
{
$f3 = Base::instance();
$this->f3 = $f3;
}
protected function get_db_name()
{
return $this->db_name;
}
protected function get_db_host()
{
return $this->db_host;
}
protected function get_db_username()
{
return $this->db_username;
}
protected function get_db_password()
{
return $this->db_password;
}
protected function set_db_name($db_name)
{
$this->db_name = $db_name;
}
protected function set_db_host($db_host)
{
$this->db_host = $db_host;
}
protected function set_db_username($db_username)
{
$this->db_username = $db_username;
}
protected function set_db_password($db_password)
{
$this->db_password = $db_password;
}
} // /class Controller
还有一个 FrontendController Class:
<?php
class FrontendController extends Controller
{
public function get_view()
{
$this->set_db_host('localhost');
}
}
他的工作是管理前端网站(html、从数据库打印结果等等)。
有一天,我需要 BackendController class,来管理站点的新闻(例如)。
1) 我需要重写数据库连接(例如)?或者我会在 Controller 的构造中移动它们?
2) 实际上,从第 1 点开始,我确实想将它们添加到 subclass 的 __construct 中,但当然它会覆盖 abstract Class 的 __construct
3) 明天我想创建一个 ToolboxController Class。假设它的范围将执行某些任务(例如 "extract first X characters from a text"、"format data according to user settings"),我 will/can 在应用程序的其他部分重用的所有代码片段。
在这种情况下,我将需要重构所有网站更改自:
Controller <= FrontendController
Controller <= BackendController
到
Controller <= ToolboxController <= FrontendController
Controller <= ToolboxController <= BackendController
其中 <= 表示 "extends" ?
这个问题看起来很愚蠢也很简单,但是我会理解将一个天生的小网站添加到一个大型门户网站的新功能的逻辑过程。
你做错了。你问题的根源是,你没有模型。
一个Model并不是一个简单的class,它作为一个整体的抽象层。
- 域对象
- 值对象
- 数据库映射器
- 服务
阅读 this 好答案,了解它。
最后,你的目标应该是这样的
$model = new Model();
$controller = new Controller($model);
$view = new View($model);
View
和 Controller
共享相同的 Model
,他们明白了。他们不知道如何获取。
序言:我使用 F3 框架 (www.fatfreeframework.com)。
假设我有一个摘要 class "Controller"
<?php
abstract class Controller
{
/* F3 */
protected $f3;
/* DATABASE SECTION */
protected $db_name;
protected $db_host;
protected $db_username;
protected $db_password;
function __construct()
{
$f3 = Base::instance();
$this->f3 = $f3;
}
protected function get_db_name()
{
return $this->db_name;
}
protected function get_db_host()
{
return $this->db_host;
}
protected function get_db_username()
{
return $this->db_username;
}
protected function get_db_password()
{
return $this->db_password;
}
protected function set_db_name($db_name)
{
$this->db_name = $db_name;
}
protected function set_db_host($db_host)
{
$this->db_host = $db_host;
}
protected function set_db_username($db_username)
{
$this->db_username = $db_username;
}
protected function set_db_password($db_password)
{
$this->db_password = $db_password;
}
} // /class Controller
还有一个 FrontendController Class:
<?php
class FrontendController extends Controller
{
public function get_view()
{
$this->set_db_host('localhost');
}
}
他的工作是管理前端网站(html、从数据库打印结果等等)。
有一天,我需要 BackendController class,来管理站点的新闻(例如)。
1) 我需要重写数据库连接(例如)?或者我会在 Controller 的构造中移动它们?
2) 实际上,从第 1 点开始,我确实想将它们添加到 subclass 的 __construct 中,但当然它会覆盖 abstract Class 的 __construct
3) 明天我想创建一个 ToolboxController Class。假设它的范围将执行某些任务(例如 "extract first X characters from a text"、"format data according to user settings"),我 will/can 在应用程序的其他部分重用的所有代码片段。
在这种情况下,我将需要重构所有网站更改自:
Controller <= FrontendController
Controller <= BackendController
到
Controller <= ToolboxController <= FrontendController
Controller <= ToolboxController <= BackendController
其中 <= 表示 "extends" ?
这个问题看起来很愚蠢也很简单,但是我会理解将一个天生的小网站添加到一个大型门户网站的新功能的逻辑过程。
你做错了。你问题的根源是,你没有模型。
一个Model并不是一个简单的class,它作为一个整体的抽象层。
- 域对象
- 值对象
- 数据库映射器
- 服务
阅读 this 好答案,了解它。
最后,你的目标应该是这样的
$model = new Model();
$controller = new Controller($model);
$view = new View($model);
View
和 Controller
共享相同的 Model
,他们明白了。他们不知道如何获取。