Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的设计模式和OOP?_Php_Oop_Design Patterns - Fatal编程技术网

PHP中的设计模式和OOP?

PHP中的设计模式和OOP?,php,oop,design-patterns,Php,Oop,Design Patterns,我一直在使用PHP和其他一些OOP语言,但仍然想知道设计模式和OOP在PHP和类似语言中的含义 我有一些模块在我的项目:新闻,论坛。。。 每个模块都可以通过调用main类进行访问 主类将页面~>指向新闻/文章, 新闻/分类 具有一些模块设置/变量 每个模块中的子类处理这些特定的特性/页面 这种型号的最佳设计模式是什么?创建类的实例还是它的子类 现在,我初始化一个类,并访问子类静态方法,如下面的示例所示。但是我读过使用静态方法可以减慢代码的速度吗 class news { __constr

我一直在使用PHP和其他一些OOP语言,但仍然想知道设计模式和OOP在PHP和类似语言中的含义

我有一些模块在我的项目:新闻,论坛。。。 每个模块都可以通过调用main类进行访问

  • 主类将页面~>指向新闻/文章, 新闻/分类
  • 具有一些模块设置/变量
每个模块中的子类处理这些特定的特性/页面

这种型号的最佳设计模式是什么?创建类的实例还是它的子类

现在,我初始化一个类,并访问子类静态方法,如下面的示例所示。但是我读过使用静态方法可以减慢代码的速度吗

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
,为每个类使用名称空间,并基于名称空间创建文件夹结构