基于 PHP 的“框架”的 OOP 工作流程
我正在为 PHP 编写一小组框架结构(不要与 MVC 风格框架混淆),我想验证我的方法,因为它看起来非常“笨拙”:
所有请求都通过重定向指令(apache htaccess)传送到引导程序).
引导程序需要所有必需的文件。
然后根据路由调用用户类(这很好用,这里不包括)
每个用户模块都扩展核心模块,进而实例化支持类,例如 DB 和 CACHE,从而继承实例,我不必关心打开/管理连接,或者是否实例化这些类中的其他类 - 我没有将我的数据库/缓存作为参数传递。另外,如果我有一个很大的请求,并且在同一请求中调用大量用户类方法 - 我认为它只会使用 core.module 的一个实例 - 这应该是一件好事。正确的?正确的?
然而,配置传递似乎是不确定的:我充满激情地讨厌 GLOBALS - 但我无法创建静态配置类,因为我无法将数组放在那里(PHP 中没有静态数组)。另外,这破坏了所有 IDE 智能感知,开发人员对我很生气:(
整个方法有意义吗?我应该更多地使用 RTM 吗?
感谢您的诚实意见,伪代码附在下面。
//my_config_prod.php
<?php
$CONFIG = array('db' => array(..multi-level array here..), 'cache' => array(...));
?>
//bootstraper.php
<?php
require '../config/my_config_prod.php';
require '../core/core.mysql.php';
require '../core/core.cache.php';
require '../core/core.module.php';
require '../class/class.user.php';
//initialize things:
$GLOBALS['DB'] = new CoreMysql($CONFIG['db']);
$GLOBALS['CACHE'] = new CoreCache($CONFIG['cache']);
//run, this is simplified - route handler is installed here that will
//call appropriate module based on the incoming request
$user = new User();
$info = $user->load_user(1);
?>
//core.module.php
<?php
public function __construct () {
global $CACHE;
$this->cache = $CACHE;
global $DB;
$this->db = $DB;
}
?>
//class.user.php
<?php
class User extends CoreModule{
public function __construct() {
parent::__construct();
}
public function load_user($id) {
return $this->db->get_user($id)
}
}
?>
am writing a small set of framework structures for PHP (not be confused with a MVC style framework) and I would to like to validate my approach because it seems very "clumsy":
All requests are piped to a bootstraper through redirect directive (apache htaccess).
All necessary files are required by the bootstrapper.
Then user classes are called based on routes (this works fine, not included here)
Each user module extends core module, that in turn instantiates support classes like DB and CACHE, thus inherits the instances and I don't have to care about opening/managing connections or if I instantiate other classes within those classes - I don't have to pass my db/cache as arguments. Also, if I have a large request with plenty of user class methods calls within the same request - I think it would only use one instance of the core.module - which should be a good thing. Right? Right?
However, the config passing seems to be iffy: I hate GLOBALS with passion - BUT I cant make a static config class because I can not put the array there (no static arrays in PHP). Also, this breaks all the IDE intellisenses and the devs are mad at me :(
Does the whole approach make sense? Should I go RTM more?
Thank you for your honest opinions, the pseudo-code is attached below.
//my_config_prod.php
<?php
$CONFIG = array('db' => array(..multi-level array here..), 'cache' => array(...));
?>
//bootstraper.php
<?php
require '../config/my_config_prod.php';
require '../core/core.mysql.php';
require '../core/core.cache.php';
require '../core/core.module.php';
require '../class/class.user.php';
//initialize things:
$GLOBALS['DB'] = new CoreMysql($CONFIG['db']);
$GLOBALS['CACHE'] = new CoreCache($CONFIG['cache']);
//run, this is simplified - route handler is installed here that will
//call appropriate module based on the incoming request
$user = new User();
$info = $user->load_user(1);
?>
//core.module.php
<?php
public function __construct () {
global $CACHE;
$this->cache = $CACHE;
global $DB;
$this->db = $DB;
}
?>
//class.user.php
<?php
class User extends CoreModule{
public function __construct() {
parent::__construct();
}
public function load_user($id) {
return $this->db->get_user($id)
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的全局变量是一种注册表模式。您可以将配置存储在对象而不是数组中,这将是更好的方法。
要获得最佳参考,请查看 Zend Framework 2 配置、引导程序和应用程序资源。
Your globals are kind of registry pattern. You may store the config in object instead of the array, this would be a better approach.
For the best reference, take a look at Zend Framework 2 configs, bootstraps and application resources.
您可以使用 phpDoc 在许多 IDE 中手动添加智能感知。您是否考虑过使用 DIC 和/或类自动加载器来共享资源?您不必为每个页面请求包含所有内容。
http://php.net/manual/en/language.oop5.autoload.php
http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.method.pkg.html
You can use phpDoc to add intellisense manually in many IDEs. Have you considered using a DIC and/or class autoloader for shared resources? You shouldn't have to include everything for every page request.
http://php.net/manual/en/language.oop5.autoload.php
http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.method.pkg.html