0

I have a array with about 300 indexes and each index has about 8 "sub-indexes"(?). So it is a large(ish) array. I am working on converting my code to oop style and one of my classes(colors) will need this array passed as an argument. So my question is simple....if I create 100 color classes and pass each the array I am not creating 100 separate arrays correct just 100 pointers?

$colors['Apricot'] = array(250,180,160,3341,328,826,194,3332,0);
$colors['Apricot, Light'] = array(255,230,225,3824,8,833,2605,-1,1);

$x=new color();
$y=new color();
$z=new color();

$x->doSomething($colors);
$y->doSomething($colors);
$z->doSomething($colors);

Thee is still only one copy of the array, not three?

Thank you, Todd

2
  • The name of a class, as well as its constructor, should begin with an uppercase letter... just saying... Commented Dec 27, 2011 at 1:47
  • Pass by Reference Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. Commented Dec 27, 2011 at 2:01

2 Answers 2

4

True. But in theory the array is copied as value. It's just that PHP-internally it will not be duplicated in memory ... unless one of your objects starts to modify that passed array.

In which case you could pass an explicit & reference rather, or even convert it into an ArrayObject prior. (Again: practically unnecessary if you don't plan on editing the array.)

Sign up to request clarification or add additional context in comments.

8 Comments

Are you sure about this? Sounds very reasonable from a memory-efficient point of view of the PHP engine, but still, I'm curious how you know this? Do you have any reference to this information?
Can't find a really informative duplicate right now. We should summon NikiC, he knows such things. But we had some previous discussions like this, and it's also mentioned (albeit super cursorily) somewhere in the manual. The particular PHP behavior is commonly called "copy on write", and might have something to do with that reference counting thing.
Hey thanks for this. Using the "copy on write" you mentioned, I was also able to find this somewhat hidden reference in the documentation about it (perhaps there's even more): php.net/manual/en/function.debug-zval-dump.php#example-4713 (the explanation under the example). So that's a +1 from me. :D
Thank you very much. I believe this answers my question. I do not need to edit the array. Is passing an array 'pointer' as a parameter the best way for the class to access the data in the array?
@maddogandnoriko: Well, the simple workaround is making it a global variable. Might get me roasted here. But this is very much configuration data. Just give it a more unique name global $color_table; (better yet an entry in a data storage array) and then use that in that class instead of a passed parameter.
|
1

Yes, you are creating 100 copies, because scalar and array arguments are, by default, passed by value, and not by reference, to class methods in PHP.

In order for the method to take in the argument by reference you'd have to change the method signature by prepending an ampersand to the parameter name, like so:

public function doSomething( &$argument )
{
}

edit:
For a more accurate account of the internal workings, please see mario's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.