2

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
5
  • 1
    Can arrays be created using Array()(using upper case)? The manual says array()(using lower case) Commented Aug 13, 2012 at 3:40
  • 1
    @AzizAG PHP is case-insensitive for that. Commented Aug 13, 2012 at 3:45
  • @PauloFreitas aRrAy('makes','me','sad :(') Commented Aug 13, 2012 at 3:48
  • @PauloFreitas It actually works, I just tried it, I'm impressed. Commented Aug 13, 2012 at 3:48
  • @AzizAG I'm not sure if "impressed" is the correct word for this .. (perhaps "surprised"?) Commented Aug 13, 2012 at 3:49

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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.

1 Comment

My object oriented brain wouldn't conceive that array() is a function
1

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++ :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.