Skip to content
Advertisement

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

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

JavaScript

This results in a fatal error:

JavaScript

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

JavaScript

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?

Advertisement

Answer

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

JavaScript

It looks like this when you type it:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement