Tyler
Tyler

Reputation: 19848

Why 'Array()' and not 'new Array()' in 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

Upvotes: 2

Views: 1121

Answers (4)

khaverim
khaverim

Reputation: 3554

Unless you create the "Array" class, it will 'blow up' because PHP doesn't have a built-in Array class.

Array is not a class, but rather a function.

For the same reason you can't do

$myvar = new print('hello');

That would be crazy, no? print(), like array(), is a function built into PHP, not a class.

Upvotes: 2

Hawili
Hawili

Reputation: 1659

Array was in php before even object oriented ways was introduced in it

Think of it as C language array which can still be used in C++ :)

Upvotes: 1

Musa
Musa

Reputation: 97707

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

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799014

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

Upvotes: 2

Related Questions