Tyl*_*ter 6 php oop wordpress codeigniter
所以....
显然,很多问题都是关于单身人士,全球状态变量以及所有伟大的东西.我的问题是,
如果Singletons和Globals太糟糕了,他们为什么经常这么用?
下面的例子很简单,我相信很多人都会使用这些例子.
我从CodeIgniter给你一个使用伪单例函数的函数:
(system\codeigniter\Common.php第89行)
/**
* Class registry
*
* This function acts as a singleton. If the requested class does not
* exist it is instantiated and set to a static variable. If it has
* previously been instantiated the variable is returned.
*
* ......
*/
function &load_class($class, $instantiate = TRUE)
{
static $objects = array();
// Does the class exist? If so, we're done...
if (isset($objects[$class]))
{
return $objects[$class];
}
.......
}
Run Code Online (Sandbox Code Playgroud)
通过将每个对象放入单个注册表中,您无法使用其load_class函数来创建任何内容的多个实例.当您想要将类用作数据结构时,这尤其不方便.
此外,因为所有这些类只有一个实例,所以它会导致反对全球状态的论点.这导致我.....
整个Wordpress系统,主要运行全局变量.循环遍历帖子的所有数据都散布在各种全局变量中.
(wp-includes\query.php第2644行)
/**
* Setup global post data.
*
*....
*/
function setup_postdata($post) {
global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
$id = (int) $post->ID;
$authordata = get_userdata($post->post_author);
....
}
Run Code Online (Sandbox Code Playgroud)
这些只是使用Singleton/Globals作为整个系统基础的框架的两个主要示例!
那么..是因为这些系统还没有赶上OOP方法吗?当你有这么多人告诉你不要使用全局变量或单身,根据所述实践制作整个系统时,这是没有意义的.
当然有关于向后兼容PHP4的论点.我仍然认为有很多方法可以在PHP4中进行OOP编程,因为类仍然可用.
| 归档时间: |
|
| 查看次数: |
3126 次 |
| 最近记录: |