简体   繁体   English

什么是更好的方法?

[英]What is better approach?

I am working on a PHP framework. 我正在研究PHP框架。 This framework initiates by making an object $registry in which it keeps storing libraries like: 该框架通过创建一个对象$registry来启动,该对象不断存储如下库:

$registry->set('document',new Document());
$registry->set('url', new Url());

There are some libraries which need $registry for their operations. 有些图书馆的运作需要$registry So this framework passes entire $registry to their constructors. 因此,此框架将整个$registry传递给其构造函数。 Like: 喜欢:

$upload = new Upload($registry); //registry gets stored in a private var
$registry->set('upload',$upload);

And there are many libraries like this. 并且有很多这样的库。 They store a copy of $registry inside. 他们在内部存储$registry的副本。

My question is, does it really affect memory by passing a $registry to these libraries again and again, kind of redundancy? 我的问题是,通过将$ registry一次又一次地传递给这些库,是否确实会影响内存,这是一种冗余? If yes, how can I avoid it? 如果可以,我该如何避免呢?

in php you can pass a reference : http://www.php.net/manual/fr/language.references.php but i'm not even sure that you will meet a memory problem. 在php中,您可以传递参考: http : //www.php.net/manual/fr/language.references.php,但我什至不确定您是否会遇到内存问题。

and eventually could your required variable be statics ? 最终您所需的变量可以是静态变量吗?

It won't affect memory. 它不会影响内存。 As long as you're passing an object, it will get passed either by reference (if you use the & operand) or by identifier (without & ). 只要您传递一个对象,它就会通过引用(如果使用操作数)或通过标识符(不带 )传递。 Since you're not creating a new zval container (like you would when you pass something by value and start changing it) the memory should be OK. 由于您没有创建新的zval容器(就像按值传递某些东西并开始更改它那样),因此内存应该可以。

You should be careful though, as the two methods will work differently in some circumstances. 不过,您应该小心,因为在某些情况下这两种方法的工作方式会有所不同。 Take a look at this excellent answer to see the exact difference between them. 看看这个出色的答案 ,看看它们之间的确切区别。 But as a rule of thumb, if you want to pass something around and make changes to it, always pass it using the &. 但是,根据经验,如果要传递某些内容并对其进行更改,请始终使用&传递它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM