简体   繁体   中英

What is better approach?

I am working on a PHP framework. This framework initiates by making an object $registry in which it keeps storing libraries like:

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

There are some libraries which need $registry for their operations. So this framework passes entire $registry to their constructors. 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.

My question is, does it really affect memory by passing a $registry to these libraries again and again, kind of redundancy? 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.

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.

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 &.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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