框架中的 PHP 对象引用
在进入讨论部分之前,先问一个简单的问题;有没有一种方法可以确定一个变量是否是对另一个变量/对象的引用? (只是想检查我是否正确传递引用,而不是复制我的对象的版本)。例如,
$foo = 'Hello World';
$bar = &$foo;
echo (is_reference($bar) ? 'Is reference' : 'Is orginal';
我已经使用 PHP5 几年了(仅限个人使用),我想说我在面向对象实现的主题上有一定程度的逆转。然而,模型视图控制器框架的概念对我来说相当新。
我查看了许多教程并查看了一些开源框架(主要是 CodeIgnitor),以更好地理解所有内容如何组合在一起。我开始意识到使用这种结构的真正好处。
我习惯于通过以下技术实现对象引用。
class Foo{
public $var = 'Hello World!';
}
class Bar{
public function __construct(){
global $Foo;
echo $Foo->var;
}
}
$Foo = new Foo;
$Bar = new Bar;
我很惊讶地发现 CodeIgnitor 和 Yii 传递对象的引用,并且可以通过以下方法访问:
$this->load->view('argument')
我可以看到的直接优势是代码少得多,并且对用户更加友好。但我确实想知道它是否更有效,因为这些框架可能已经过优化?或者只是为了让代码更加用户友好?或者还有其他什么好处?这是一篇有趣的文章不要使用 PHP 引用。
我问这个问题的原因是我正在尝试为个人项目和学习曲线构建一个框架。
更新
$Baz = $Foo;
$Baz->var = 'Goodbye World!';
echo $Foo->var;
/* Ouput */
Goodbye World!
我有点不好意思地说我没想到会给出这样的输出。当然现在确实让事情变得容易多了。
Before I dive into the disscusion part a quick question; Is there a method to determine if a variable is a reference to another variable/object? (Just want to check I am passing references around correctly and not make duplicate versions of my objects). For example
$foo = 'Hello World';
$bar = &$foo;
echo (is_reference($bar) ? 'Is reference' : 'Is orginal';
I have been using PHP5 for a few years now (personal use only) and I would say I am moderately reversed on the topic of Object Orientated implementation. However the concept of Model View Controller Framework is fairly new to me.
I have looked a number of tutorials and looked at some of the open source frameworks (mainly CodeIgnitor) to get a better understanding how everything fits together. I am starting to appreciate the real benefits of using this type of structure.
I am used to implementing object referencing in the following technique.
class Foo{
public $var = 'Hello World!';
}
class Bar{
public function __construct(){
global $Foo;
echo $Foo->var;
}
}
$Foo = new Foo;
$Bar = new Bar;
I was surprised to see that CodeIgnitor and Yii pass referencs of objects and can be accessed via the following method:
$this->load->view('argument')
The immediate advantage I can see is a lot less code and more user friendly. But I do wonder if it is more efficient as these frameworks are presumably optimised? Or simply to make the code more user friendly? Or some other advantage? This was an interesting article Do not use PHP references.
The reason I ask is I am attempting to put a framework together for a personal project and for the learning curve.
UPDATE
$Baz = $Foo;
$Baz->var = 'Goodbye World!';
echo $Foo->var;
/* Ouput */
Goodbye World!
I am a little embarassed to say I was not expecting this to give this output. Certainly does make things a lot easier now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的&运算符是 PHP4 通过引用传递对象的方式,PHP5 默认情况下这样做。仍使用此运算符的代码将向后兼容。
您仍然可以使用 &在 PHP5 中通过引用传递其他数据类型,但这种需要非常罕见,我建议避免它。
The & operator is the PHP4 way to pass objects by reference, PHP5 does this by default. Code that still uses this operator is ment to be backward compatible.
You can still use & to pass other datatypes by reference in PHP5, but the need for it is very rare and i suggest to avoid it.