I'm searching to the best & most elegant way to have, in symfony2 project, 
functions available at global scope, ie, you can access everywhere in the 
project.

Firstly, a requirement/function example:
I need to merge various "number-keyed" arrays (eg [4=>"foo", 7=>"bar"]) 
maintaing the keys, so php array_merge function isn't valid for me. 
I found this function at stackoverflow ( 
http://stackoverflow.com/questions/10083158/how-to-merge-two-php-doctrine-2-arraycollection)
function array_merge_maintain_keys() { 
    $args = func_get_args(); 
    $result = array(); 
    foreach ( $args as &$array ) { 
        foreach ( $array as $key => &$value ) { 
            $result[$key] = $value; 
        } 
    } 
    return $result; 
} 

Some ideas:
-A Trait (php 5.4): I don't like this, code is reusable, but isn't elegant
-A php extension: too much effort to add an stupid function
-Add an include_once at the end of app/autoload.php pointing to a plain php 
file with that kind of simple function(s)
-A class static methods, something like 
\Acme\Utils::array_merge_mantain_keys();
-A service is another option, but if I need to call that function from an 
entity I would have to inject it previously (I hate this)

Which would be the best & the most elegant solution? 

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to