<code><?php namespace app\models; use yii\db\ActiveRecord; class Country extends ActiveRecord { } </code>
This is the model class definition code
<code><?php namespace app\controllers; use yii\web\Controller; use yii\data\Pagination; use app\models\Country; class CountryController extends Controller { public function actionIndex() { $query = Country::find(); $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $query->count(), ]); $countries = $query->orderBy('name') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('index', [ 'countries' => $countries, 'pagination' => $pagination, ]); } }</code>
**This is the controller code that calls the model class
Excuse me, the namespace in the model class is appmodels, so why does the controller use appmodelsCountry instead of use appmodels?
Is this because there are other use statements before him or something else? I have read many tutorials about PHP namespaces but none of them mentioned this problem. Please give me some answers. **
<code><?php namespace app\models; use yii\db\ActiveRecord; class Country extends ActiveRecord { } </code>
This is the model class definition code
<code><?php namespace app\controllers; use yii\web\Controller; use yii\data\Pagination; use app\models\Country; class CountryController extends Controller { public function actionIndex() { $query = Country::find(); $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $query->count(), ]); $countries = $query->orderBy('name') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('index', [ 'countries' => $countries, 'pagination' => $pagination, ]); } }</code>
**This is the controller code that calls the model class
Excuse me, the namespace in the model class is appmodels, so why does the controller use appmodelsCountry instead of use appmodels?
Is this because there are other use statements before him or something else? I have read many tutorials about PHP namespaces but none of them mentioned this problem. Please give me some answers. **
The namespace of
php does not support the *
method. If c#
supports use app.models.*
, all classes under the model can be introduced, but php does not support it (see the autoload
method for details , you will understand after reading it), it must be introduced into a specific class
<code>namespace Your\Namespace\Name; use It\Is\Class\Name; //调用这个类,而不是调用一个命名空间 use It\Is\Class\MyClass AS OuterClass; //因为MyClass和声明的类重名,使用AS定义别名 /** * 全名 \Your\Namespace\Name\MyClass * extend Name 等价于 extend \It\Is\Class\Name,如果你不写use It\Is\Class\Name;这句的话 */ class MyClass extend Name { use Alert; //调用方法 public function demo() { $obj = new OuterClass(); //相当于 new \It\Is\Class\MyClass() $obj = new Object(); //代表 \Your\Namespace\Name\Object; $obj = new Demo\Object(); //代表 Your\Namespace\Name\Demo\Object; //根据以上两句你应该明白了,namespace是指明该文件所处的命名空间,如果按照你的理解 use 后面是命名空间的话,那么当前文件就同时隶属于不同的命名空间了,会发生混乱。 } } trait Alert { //全名为\Your\Namespace\Name\Alert public function vardump() { var_dump($this); } } $obj = new MyClass(); $obj->vardump();</code>
Note:
in use
php can import 类
, 命名空间
or 接口
.
So, it is more convenient to directly introduce a class here.