Can PHP's interactive mode run code in a namespace?

Leo Galleguillos :

While in PHP's interactive mode, I tried the following:

php > namespace MyNamespace;
php > class Throwable {}

This results in a fatal error:

PHP Fatal error:  Cannot declare class Throwable, because the name is already in use in php shell code on line 1

However, the following code, when in a PHP file, executes without errors:

<?php
namespace MyNamespace;
class Throwable {}

Therefore, is it possible to somehow set a namespace while in interactive mode? Or, is all code in interactive mode run in the global space regardless of a previous namespace definition?

Ja͢ck :

For this to work, you have to apply the example given in the documentation that allows for combining namespaced and unnamespaced code:

namespace MyNamespace {
    class Throwable {}
}

It looks like this when you type it:

> namespace Mynamespace {
{ class Throwable {}
{ }
>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11959&siteId=1