Ryan Tse
Ryan Tse

Reputation: 1599

PHP: Referencing Global Functions in Namespace

I came across a weird issue that I cannot explain with namespaces. The function array() does not seem to allow a global reference using \array() in namespaces. Is this expected functionality or some kind of inconsistency in PHP?

The error that is returned is: PHP Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING in php shell code on line 2

The follow piece of code replicates the error that is shown:

<?php
namespace Testing;

final class Test {
    private $properties = \array(
        "test" => "testing",
        "weird_functionality" => "test"
    );
}

?>

Upvotes: 0

Views: 96

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

array() is not a function it is a language construct and cannot being namespaced.

Use this (without the \):

<?php
namespace Testing;

final class Test {
    private $properties = array(
        "test" => "testing",
        "weird_functionality" => "test"
    );
}

Upvotes: 0

deceze
deceze

Reputation: 522024

array() is not a function, it's basically a language primitive ("language construct"). As you see, it even has its own parser token T_ARRAY.

So yes, that's expected, since it's not affected by namespacing to begin with.

Upvotes: 1

Related Questions