2

Here is my code.

defined( 'ABSPATH' ) || exit;

namespace JSR;

class myClass{
...
}

This is giving below error

Global code should be enclosed in global namespace declaration

Any idea how to fix it?

3

From the PHP manual on defining namespaces:

Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.

To fix the issue, simply make sure that your namespace declaration comes before the other code:

namespace JSR;

defined( 'ABSPATH' ) || exit;

class myClass{

}
0

You can use "Use" keyword, to access your class of that namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.