Best way to initialize (empty) array in PHP -


in other languages (as3 example), has been noted initializing new array faster if done var foo = [] rather var foo = new array() reasons of object creation , instantiation. wonder whether there equivalences in php?

class foo {     private $arr = array(); // there / better way? } 

in ecmascript implementations (for instance, actionscript or javascript), array() constructor function , [] part of array literal grammar. both optimized , executed in different ways, literal grammar not being dogged overhead of calling function.

php, on other hand, has language constructs may functions aren't treated such. even php 5.4, supports [] alternative, there no difference in overhead because, far compiler/parser concerned, synonymous.

// before 5.4, write $array = array(     "foo" => "bar",     "bar" => "foo", );  // of php 5.4, following synonymous above $array = [     "foo" => "bar",     "bar" => "foo", ]; 

if need support older versions of php, use former syntax. there's argument readability but, being long-time js developer, latter seems rather natural me.  made mistake of trying initialise arrays using [] when first learning php.

this change language proposed , rejected due majority vote against core developers following reason:

this patch not accepted because slight majority of core developers voted against. though if take accumulated mean between core developers , userland votes seems show opposite irresponsible submit patch witch not supported or maintained in long run.

however, appears there change of heart leading 5.4, perhaps influenced implementations of support popular databases mongodb (which use ecmascript syntax).


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -