PHP中的设计模式和OOP?
我一直在使用PHP和其他一些OOP语言,但仍然想知道设计模式和OOP在PHP和类似语言中的含义 我有一些模块在我的项目:新闻,论坛。。。 每个模块都可以通过调用main类进行访问PHP中的设计模式和OOP?,php,oop,design-patterns,Php,Oop,Design Patterns,我一直在使用PHP和其他一些OOP语言,但仍然想知道设计模式和OOP在PHP和类似语言中的含义 我有一些模块在我的项目:新闻,论坛。。。 每个模块都可以通过调用main类进行访问 主类将页面~>指向新闻/文章, 新闻/分类 具有一些模块设置/变量 每个模块中的子类处理这些特定的特性/页面 这种型号的最佳设计模式是什么?创建类的实例还是它的子类 现在,我初始化一个类,并访问子类静态方法,如下面的示例所示。但是我读过使用静态方法可以减慢代码的速度吗 class news { __constr
- 主类将页面~>指向新闻/文章, 新闻/分类李>
- 具有一些模块设置/变量
class news {
__construct() {
// define module settings
...
// routing all module pages
categories::list();
}
function modulesetting() {}
}
class categories extends news {
static function list() {}
}
只是为了表明我的意思(放轻松):
文件夹结构:
/index.php
/autoload.php
/IModule.php
/IPage.php
/BaseModule.php
/modules/
/modules/News.php
/modules/News/
/modules/News/Categories.php
/modules/News/Acticle.php
...
index.php
require_once('./autoload.php');
// based on request select which module to use
$moduleName = 'News';
$page = ('module\\'.$moduleName)::route();
$page->render();
autoload.php
function __autoload($classname) {
$filename = __DIR__ . '/'. $classname . '.php';
include_once($filename);
}
IModule.php
interface IModule {
public static function route();
//protected function moduleSettings();
}
abstract class BaseModule implements \IModule {
// fundamental stuff
}
IPage.php
interface IPage {
public function render();
}
BaseModule.php
interface IModule {
public static function route();
//protected function moduleSettings();
}
abstract class BaseModule implements \IModule {
// fundamental stuff
}
News.php
namespace modules;
class News extends \BaseModule {
protected $someProperty;
__construct() {
// define module settings
}
public static function route(){
// routing all module pages, based on request
$page = 'modules\News\Categories';
return new $page;
}
protected function moduleSettings(){ ... }
}
Categories.php
namespace modules\News;
class Categories extends \module\News implements \IPage {
public function render() {
/* access to module news */
$this->someProperty;
$this->moduleSettings(); /* or if method moduleSettings is overwriten then */ parent::moduleSettings();
/* for example */
echo '<html></html>';
}
}
名称空间模块\新闻;
类类别扩展\module\News实现\IPage{
公共职能{
/*访问模块新闻*/
$this->someProperty;
$this->moduleSettings();/*或者如果方法moduleSettings被覆盖,则*/parent::moduleSettings();
/*例如*/
回声';
}
}
只是为了说明我的意思(放轻松):
文件夹结构:
/index.php
/autoload.php
/IModule.php
/IPage.php
/BaseModule.php
/modules/
/modules/News.php
/modules/News/
/modules/News/Categories.php
/modules/News/Acticle.php
...
index.php
require_once('./autoload.php');
// based on request select which module to use
$moduleName = 'News';
$page = ('module\\'.$moduleName)::route();
$page->render();
autoload.php
function __autoload($classname) {
$filename = __DIR__ . '/'. $classname . '.php';
include_once($filename);
}
IModule.php
interface IModule {
public static function route();
//protected function moduleSettings();
}
abstract class BaseModule implements \IModule {
// fundamental stuff
}
IPage.php
interface IPage {
public function render();
}
BaseModule.php
interface IModule {
public static function route();
//protected function moduleSettings();
}
abstract class BaseModule implements \IModule {
// fundamental stuff
}
News.php
namespace modules;
class News extends \BaseModule {
protected $someProperty;
__construct() {
// define module settings
}
public static function route(){
// routing all module pages, based on request
$page = 'modules\News\Categories';
return new $page;
}
protected function moduleSettings(){ ... }
}
Categories.php
namespace modules\News;
class Categories extends \module\News implements \IPage {
public function render() {
/* access to module news */
$this->someProperty;
$this->moduleSettings(); /* or if method moduleSettings is overwriten then */ parent::moduleSettings();
/* for example */
echo '<html></html>';
}
}
名称空间模块\新闻;
类类别扩展\module\News实现\IPage{
公共职能{
/*访问模块新闻*/
$this->someProperty;
$this->moduleSettings();/*或者如果方法moduleSettings被覆盖,则*/parent::moduleSettings();
/*例如*/
回声';
}
}
使用静态方法比类的实例慢一点,但另一方面,静态方法节省内存。我看到您的子类类别
继承类新闻
,因此您不需要类的实例新闻
,因为类别的实例
继承方法构造等。您可以覆盖每个子类的方法列表
和模块设置
,有一些方法可以从被覆盖的方法调用父方法,如parent::list()代码>或父::模块设置()代码>还有一个想法是,例如,您可以在新闻->\u构造:如果($this instanceof categories){…}
如果您为处理路由的所有主类创建抽象基类,并为所有子类添加基本控件,这是我的opinion@Kazz我考虑过这个解决方案:直接调用child方法,避免创建news
实例。但是,我不想在索引
文件中放太多代码,这些文件是许多子类中的一个类别。我希望索引
文件只通过一个文件与每个模块联系,新闻模块的新闻实例,论坛模块的论坛实例,路由作业属于每个模块。。。如果您在路上,我必须更改路由到索引
文件。这里最好的解决方案是什么?创建\uu autoload
函数将其放入单独的文件,如autoload.php
,然后在每个文件中,需要访问这些类的人包括autoload.php
,为每个类使用名称空间并基于名称空间创建文件夹结构使用静态方法比类的实例慢一点,但另一方面静态方法节省内存。我看到您的子类类别
继承类新闻
,因此您不需要类的实例新闻
,因为类别的实例
继承方法构造等。您可以覆盖每个子类的方法列表
和模块设置
,有一些方法可以从被覆盖的方法调用父方法,如parent::list()代码>或父::模块设置()代码>还有一个想法是,例如,您可以在新闻->\u构造:如果($this instanceof categories){…}
如果您为处理路由的所有主类创建抽象基类,并为所有子类添加基本控件,这是我的opinion@Kazz我考虑过这个解决方案:直接调用child方法,避免创建news
实例。但是,我不想在索引
文件中放太多代码,这些文件是许多子类中的一个类别。我希望索引
文件只通过一个文件与每个模块联系,新闻模块的新闻实例,论坛模块的论坛实例,路由作业属于每个模块。。。如果您在路上,我必须更改路由到索引
文件。这里最好的解决方案是什么?创建\uu autoload
函数将其放入单独的文件,如autoload.php
,然后在需要访问这些类的每个文件中包括autoload.php
,为每个类使用名称空间,并基于名称空间创建文件夹结构