vote up 0 vote down star

Hi all, Im currently building a php framework... again.

I have a class called config.

its pretty simple, its called like so:

$conf = config::get('general');

$conf is now an array full of config goodies.

the class sceleton is like so:

final class config {

private static $configs = array();

public static function get($name) {

return self::$configs[$name];

}

}

assume the $configs array is already populated and has a "general" key.

This "general" key holds an array that is exactly 1 megabyte.

Lets say I call

config::get('general');

10 times into different variables. None of the variables are edited afterwards... does this mean i have 10 variables each containing 1 megabyte or 10 variables pointing to 1 megabyte?

flag

78% accept rate

2 Answers

vote up 3 vote down check

Only one, if you do not modify them.

But php is not the language where you can rely on any particular behavior ;-)

Just tried:

<?php

printf("%10d\n",memory_get_usage());
$a = array_fill(0,30000,'oh');
printf("%10d\n",memory_get_usage());
$b = $a;
printf("%10d\n",memory_get_usage());
$b[] = '';
printf("%10d\n",memory_get_usage());

output:

    325524
   2256916
   2256980
   4188316
link|flag
Great advice, NEVER trust a person or documentation's advice on memory usage until you prove it yourself. PHP is constantly changing and what was true a couple years ago might not be true anymore. As a heads up, if you closely watch memory usage, you might not be happy how inefficient PHP5 is with OOP code. But of course it always depends on what you're doing. – TravisO 2 days ago
TravisO, this not the only thing I'm not happy about when it comes to PHP ;-) – Michael Krelin - hacker 2 days ago
@michael: you made a typo, you meant "you can't rely on" – TravisO 2 days ago
@Michael: I try to avoid OOP when I code PHP, I've been hoping PHP6 would be much better but I haven't tried any of the betas yet and measured memory usage. PHP 5.3 didn't make any noticeable difference over 5.2 but that's not a major release like 6 will be. – TravisO 2 days ago
No, no typo. I said php is not the language where you can ;-) And I loathe PHP since 2.0 and have no hopes whatsoever. – Michael Krelin - hacker 2 days ago
vote up -1 vote down

My understanding is that as you have written it you would get 10 arrays each 1MB.

If you us & to either pass in a by-reference variable and return a pointer or use & in front of the function name to return a by reference return value you may be able to do what you want.

link|flag
i always thought php class properties were referenced tho :S – Ozzy 2 days ago
i may be wrong. Try editing the array you get back and see if it changes in your other references that would tell you if its referenced. – Toby Allen 2 days ago
Ozzy, I think you mean that objects are passed by references, not properties. – Michael Krelin - hacker 2 days ago

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.