Home > Article > Backend Development > opencart analysis_PHP tutorial
After studying opencart for two days, I will make a summary of its implementation principles and discuss it with you:
opencart is a self-developed architecture, and its implementation idea is also based on MVC. The core of the architecture is under system/engine, including several files,
1): controller.php The base class of all controllers
2): action.php Action steering, that is, path, for example, the class ControllerAccountAddress under catalog is for account/address
3): front.php Front-end action execution file, this is an operation based on action.php, which is equivalent to action.php loading data, and front.php is the action, responsible for execution.
4): loader.php This is to load related class library files, including files under database, model, and library. The calling method is $this->load->library("library File name ")
Other reference loader.php files, such as model, $this->load->model("account/address");
5): model.php This file is the base class of all models and will not be explained further.
6): registry.php The implementation of this file is the same as Model.php. This class Registry is the information center of the entire system. Registry is a singleton (Singleton). In the index.php start page,
First, pass the value of the class to be used as a constructor parameter to create a class instance, and then set this class instance to the "registry",
This registry is like a shared data bus, connecting various modules/data together.
There are some public classes under System, so the basic classes and public classes are loaded through index.php, which is registered by Registry, so that you can load the classes and files you need
var_dump($registry);exit; The content printed out by setting a breakpoint in index.php (the intercepted part) is shown as follows:
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 ...
}
}