0

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"
    );
}

?>

2 Answers 2

1

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.

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

Comments

0

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"
    );
}

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.