您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

opencart分析_PHP教程

2024/5/4 0:53:38发布26次查看
研究了两天opencart,对其中的实现原理做个小结,和各位共同探讨下:
opencart是自有开发的一套架构,其实现思想也是基于mvc的一种方式,架构的核心在system/engine下面,包括了几个文件,
    1):controller.php   所有控制器的基类
     2):action.php   动作转向,也就是路径,比如 catalog下面的类controlleraccountaddress就是对于account/address
   3): front.php  前端动作执行文件,这个是在action.php基础上进行的操作,也就是相当于action.php是加载了数据,而front.php是动作,负责执行的。
   4):loader.php   这个是加载相关的类库文件,包括database下的,model下的,library下的文件,调用方法就是$this->load->library(library下的文件名“)
                                        其他参考loader.php文件,比如model,$this->load->model(account/address);
  5):model.php   这个文件是所有model的基类,不作多说明。
  6):registry.php  这个文件的实现和model.php一样,这个类 registry是整个系统的信息中枢, registry是一个单例(singleton),在index.php起始页面中,
     首先作为构造函数参数传 值 所要用到的类创建类实例,并随之将这个类实例设置到这个“注册表”中,
     这个注册表就像是一个共享的数据总线一样,把各个模块/数据串联在一起。
在system下有一些公共类,所以的基础类和公共类都是通过index.php 去加载的,也就是 registry注册的,这样你就可以去加载你需要的类和文件了
    var_dump($registry);exit;在index.php设置断点打印出来的内容 (截取部分)显示如下:
object(registry)[1]
  private 'data' =>
    array
      'load' =>
        object(loader)[2]
          protected 'registry' =>
            &object(registry)[1]
      'config' =>
        object(config)[3]
          private 'data' =>
            array
              ...
      'db' =>
        object(db)[4]
          private 'driver' =>
            object(mysql)[5]
              ...
      'url' =>
        object(url)[8]
          private 'url' => null
          private 'ssl' => null
          private 'rewrite' =>
            array
              ...
      'log' =>
        object(log)[9]
          private 'filename' => string 'error.txt' (length=9)
      'request' =>
        object(request)[10]
          public 'get' =>
            array
              ...
          public 'post' =>
            array
              ...
          public 'cookie' =>
            array
              ...
          public 'files' =>
            array
              ...
          public 'server' =>
            array
              ...
          public 'request' =>
            array
              ...
      'response' =>
        object(response)[11]
          private 'headers' =>
            array
print_r($registry->get('config')->get('account_module'));exit; 这是打印单个属性的内容
下面用实例来说明:
registry.php类的声明如下:
final class registry {
private $data = array();
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : null);
}
public function set($key, $value) {
$this->data[$key] = $value;
}
public function has($key) {
     return isset($this->data[$key]);
   }
}
controller的声明如下(截取部分):
abstract class controller {
protected $registry;
protected $id;
protected $layout;
protected $template;
protected $children = array();
protected $data = array();
protected $output;
public function __construct($registry) {
$this->registry = $registry;
}
public function __get($key) {
return $this->registry->get($key);
}
public function __set($key, $value) {
$this->registry->set($key, $value);
    }
}
任意声明一些变量:
$arr=array(mantis=>task,hefei=>anhui);
$str=中国安徽合肥;
声明一个类:
class  db{
   private $xx='123456';
   private $data=array();
public function get($key) {
    return (isset($this->data[$key]) ? $this->data[$key] : $key);
   }
   function connect(){
      echo 'you are connecting...';
   }
}
//声明一个控制类:
class controlleraccountfix extends controller{
 var $name;
 var $age;
 var $key='opencat';
 function __construct(){
   $this->name='c';
   $this->age='12';
}
function fix(){
   echo $this->key.php_eol;
 }
}
//声明注册类
$reg=new registry();
注册这些数据成为公共的部分:
        $reg->set(arr,$arr);
$reg->set(str,$str);
$reg->set(class,new controlleraccountfix());
$reg->set(db,new db());
$controller = new controlleraccountfix($reg);
if (is_callable(array($controller, 'fix'))) {
$action = call_user_func_array(array($controller, 'fix'), array('dddd'));
}
//以上代码输出opencart。
在把控制类重写一下:
class controlleraccountfix extends controller{
 protected $registry; www.2cto.com
function fix(){
echo  $this->db->get('xx'); //输出123456
                  echo   $this->db->connect();//输出 you are connecting ...
 }
}
http://www.bkjia.com/phpjc/477963.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477963.htmltecharticle研究了两天opencart,对其中的实现原理做个小结,和各位共同探讨下: opencart是自有开发的一套架构,其实现思想也是基于mvc的一种方式,...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product