Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'Array()' and not 'new Array()' in PHP?

Tags:

arrays

object

php

Other objects in PHP are instantiated using the new qualifier such as new Date(), etc. How come you do not supply the new qualfier with Array() when instantiating a new array in PHP?

$array = new Array(); //blows up
$array = Array();     //works as intended

$reflect = new ReflectionClass($this);   //works as intended
$reflect = ReflectionClass($this);       //blows up
like image 490
tyler Avatar asked Sep 23 '26 23:09

tyler


2 Answers

Array is a language construct not a class so you cant use new to instantiate an Array object.

like image 178
Musa Avatar answered Sep 25 '26 13:09

Musa


Because array is syntax, not a class. Same as with list, echo, and a few others.

like image 43
Ignacio Vazquez-Abrams Avatar answered Sep 25 '26 11:09

Ignacio Vazquez-Abrams