Reputation: 1475
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?
Upvotes: 0
Views: 44
Reputation: 674
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 &.
Upvotes: 0
Reputation: 439
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 ?
Upvotes: 1