views:

64

answers:

2

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?

A: 

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.

Toby Allen
i always thought php class properties were referenced tho :S
Ozzy
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
Ozzy, I think you mean that objects are passed by references, not properties.
Michael Krelin - hacker
+3  A: 

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
Michael Krelin - hacker
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
TravisO, this not the only thing I'm not happy about when it comes to PHP ;-)
Michael Krelin - hacker
@michael: you made a typo, you meant "you can't rely on"
TravisO
@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
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