fireboy
fireboy

Reputation: 29

In yii2, loading a namespace with its full path does not work, but using `use` does

Under yii2

new yii\web\ForbiddenHttpException(); 

not work but

use yii\web\ForbiddenHttpException;
new ForbiddenHttpException();

works why?

Upvotes: 0

Views: 58

Answers (1)

Bizley
Bizley

Reputation: 18021

This is explained in docs.

Names that contain a backslash but do not begin with a backslash like my\name can be resolved in 2 different ways.

If there is an import statement that aliases another name to my, then the import alias is applied to the my in my\name.

Otherwise, the current namespace name is prepended to my\name.

So without importing you need to write it like this:

new \yii\web\ForbiddenHttpException();

Upvotes: 1

Related Questions