Jump to content

Setting the $mode Parameter of the RecursiveIteratorIterator __construct() Method


iwato
 Share

Recommended Posts

Question: When the RecursiveIteratorIterator __ construct() method is called in the following foreach-statement, its $mode parameter is set to SELF_FIRST. Why cannot this parameter simply be set by entering the parameter value SELF_FIRST? Is it because the __construct() method is inherited? I don't get it.

 function array_flatten_recursive($array) {		if($array) {			$flat = array();			foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array),				RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) {				if(!is_array($value)) {					$flat[] = $value;				}			}		   			return $flat;		} else {			return false;		}	}

Roddy

Link to comment
Share on other sites

You mean why not use a string instead?For compression (and therefore memory efficiency) mostly... With a string, the constructor needs the amount of bytes which the string's number of characters is. With a constant, the constructor needs the number of bytes the constant's value is, which can easily be an integer - 4 bytes, which is less than the string "SELF_FIRST"."And what about the memory for the constant itself?" I hear you asking... I'm not completely sure how much memory PHP uses per constant... it is sure to need at least the number of bytes in the local identifier plus the bytes in the value, so that's at least "SELF_FIRST"+4 = 14 bytes... yes, it's more, but that space is needed only once, not every time you call a function with that value. Not to mention that the allocation and deallocation of memory in itself is CPU costly, and you're only doing that once too.There's also one other minor thing... editors can auto-complete a constant name, but they can't auto-complete a string, because the possible constant names are a limited known set, whereas a string can mean any text.

Link to comment
Share on other sites

You mean why not use a string instead?
Well, this works: RecursiveIteratorIterator::SELF_FIRSTThis does not: SELF_FIRSTI read what you wrote and understood in part your arguments in favor of efficiency and auto-completion. If what you say is true, though, one can only wonder why the same approach is not applied to the application of all PHP constants and not only those of PHP extensions.Roddy
Link to comment
Share on other sites

Ah, this... well, constants are available in all scopes, so if another class defined a constant called "SELF_FIRST" or if such a constant was defined outside of a class, PHP wouldn't know which of the two constants you have in mind... if you write it like that, PHP will always try to find a constant in the global scope... The fact you're using the constant in the context of a RecursiveIteratorIterator method doesn't mean anything - in theory, you can write the literal value of the constant or a constant from another extension that has the same value (in practice, why would you?).As for why it's not applied everywhere... as Steve Ballmer would say, "Developers, Developers...". Not all developers know why they should do this. In other cases, they do, but they find it an overkill.

Link to comment
Share on other sites

if you write it like that, PHP will always try to find a constant in the global scope...
Yeah, this much I got.
Not all developers know why they should do this. In other cases, they do, but they find it an overkill.
So, I think they are a little lazy. There should be an extensive list of all constants for all extensions, so that one can easily check which constant names already exist for which values, prohibit any changes in the name-value relationship unless approved by the language's caretakers, and invent new names as they become necessary.
As for why it's not applied everywhere... as Steve Ballmer would say, "Developers, Developers...".
Too much coke and pizza!Many thanks, now I can return to Iterators. You opened up an entirely new world for me in our discussion of recursive file and folder deletion that has significantly side-tracked me in my desire to learn all string and array functions. Alas, eventually I will return.Roddy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...