PHP get_实例/了解单例模式
我已经使用了CodeIgniter,为了扩展我的PHP知识,我正在尝试创建自己的框架 问题是我需要一个与CodeIgniter get_实例函数等价的函数。但是通过我所有的搜索,我无法理解它,也不知道我是否在正确的上下文中使用它 我相信我正在寻找的是单例模式,但我就是不知道如何实现它,所以有人能帮我吗 我希望能够从content函数中访问框架的$page变量 [我想说的是,这是一个简化版本,我的编码通常比这更好..] 编辑前:PHP get_实例/了解单例模式,php,singleton,Php,Singleton,我已经使用了CodeIgniter,为了扩展我的PHP知识,我正在尝试创建自己的框架 问题是我需要一个与CodeIgniter get_实例函数等价的函数。但是通过我所有的搜索,我无法理解它,也不知道我是否在正确的上下文中使用它 我相信我正在寻找的是单例模式,但我就是不知道如何实现它,所以有人能帮我吗 我希望能够从content函数中访问框架的$page变量 [我想说的是,这是一个简化版本,我的编码通常比这更好..] 编辑前: <?php class Framework {
<?php
class Framework {
// Variables
public $page;
function __construct()
{
// For simplicity's sake..
$this->page->title = 'Page title';
$this->page->content->h1 = 'This is a heading';
$this->page->content->body = '<p>Lorem ipsum dolor sit amet..</p>';
$this->output();
}
function output()
{
function content($id)
{
// I want to get an instance of $this
// To read and edit variables
echo $this->page->content->$id;
}
?>
<html>
<head>
<title><?php echo $this->page->title ?></title>
</head>
<body>
<h1><?php content('h1') ?></h1>
<?php content('body') ?>
</body>
</html>
<?php
}
}
new Framework;
编辑后:
<?php
class Framework {
// Variables
public $page;
public static function get_instance()
{
static $instance;
$class = __CLASS__;
if( ! $instance instanceof $class) {
$instance = new $class;
}
return $instance;
}
function __construct()
{
// For simplicity's sake..
$this->page->title = 'Page title';
$this->page->content->h1 = 'This is a heading';
$this->page->content->body = '<p>Lorem ipsum dolor sit amet..</p>';
$this->output();
}
function output()
{
function content($id)
{
$FW = Framework::get_instance();
// I want to get an instance of $this
// To read and edit variables
echo $FW->page->content->$id;
}
?>
<html>
<head>
<title><?php echo $this->page->title ?></title>
</head>
<body>
<h1><?php content('h1') ?></h1>
<?php content('body') ?>
</body>
</html>
<?php
}
}
new Framework;
get实例方法通常是这样工作的
public static function getInstance() {
static $instance;
$class = __CLASS__;
if ( ! $instance instanceof $class) {
$instance = new $class;
}
return $instance;
}
设置一个静态变量,该变量的状态用static关键字保留。
设置一个指向类的变量可以使维护更容易一些。
检查$instance是否是此类中的实例化对象。
如果不是,那么我们从这个类实例化一个新对象。
返回我们刚创建的新对象或现有对象。
因此,调用Class::getInstance将在第一次调用时返回一个新对象,并且
单身人士常常因为一些原因而不受欢迎,而且
这种模式使得单元测试变得非常重要
更难,因为它引入了
将全局状态转换为应用程序
它
还应注意的是,这种模式
降低了并行性的可能性
在程序中,因为访问
多线程系统中的单线程
上下文必须序列化,例如
锁定
依赖的拥护者
我认为这是一个错误
反模式,主要是由于其使用了
私有和静态方法
有的
建议的解决方法
使用诸如
作为语言的反映,例如
Java或PHP
get实例方法通常是这样工作的
public static function getInstance() {
static $instance;
$class = __CLASS__;
if ( ! $instance instanceof $class) {
$instance = new $class;
}
return $instance;
}
设置一个静态变量,该变量的状态用static关键字保留。
设置一个指向类的变量可以使维护更容易一些。
检查$instance是否是此类中的实例化对象。
如果不是,那么我们从这个类实例化一个新对象。
返回我们刚创建的新对象或现有对象。
因此,调用Class::getInstance将在第一次调用时返回一个新对象,并且
单身人士常常因为一些原因而不受欢迎,而且
这种模式使得单元测试变得非常重要
更难,因为它引入了
将全局状态转换为应用程序
它
还应注意的是,这种模式
降低了并行性的可能性
在程序中,因为访问
多线程系统中的单线程
上下文必须序列化,例如
锁定
依赖的拥护者
我认为这是一个错误
反模式,主要是由于其使用了
私有和静态方法
有的
建议的解决方法
使用诸如
作为语言的反映,例如
Java或PHP
找到一篇关于PHP的单例模式的非常好的文章。这家伙解释得很好:找到了一篇关于PHP单例模式的不错的文章。这家伙解释得很好:谢谢你的帮助,我已经编辑了我的第一个问题,包括我认为应该去哪里的单身汉,但是我得到了这个错误:致命错误:无法重新声明以前在/home/beta2/public_html/sample.php:32 in/home/beta2/public_html/sample.php第32行声明的内容。我有点认为我误解了类和对象的工作原理。感谢您的帮助,我编辑了我的第一个问题,包括我认为应该去的单身汉,但是我得到了这个错误:致命错误:无法重新声明以前在/home/beta2/public_html/sample.php:32 in/home/beta2/public_html/sample.php第32行声明的内容。我有点认为我误解了类和对象应该如何工作。。