简体   繁体   English

CakePHP 3.x中'use'关键字的含义是什么?

[英]What does the 'use' keyword mean in CakePHP 3.x?

I am starting to learn CakePHP, and the use keyword seems to be everywhere, however I can not find documentation for it. 我开始学习CakePHP,并且use关键字似乎无处不在,但我无法找到它的文档。 Is it like import in Java? 它是否像Java中的导入? Here is an example from the CakePHP blog tutorial. 以下是CakePHP博客教程中的一个示例。

// src/Model/Table/ArticlesTable.php

namespace App\Model\Table;

use Cake\ORM\Table;

class ArticlesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
    }
}

The use keyword gives you the ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. use关键字使您能够使用别名或导入来引用外部完全限定名称,这是命名空间的一个重要特性。

All versions of PHP that support namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. 支持命名空间的所有PHP版本都支持三种别名或导入:别名类名,别名接口名和别名命名空间名。 PHP 5.6+ also allows aliasing or importing function and constant names. PHP 5.6+还允许别名或导入函数和常量名称。

Example : 示例:

use My\Full\Classname as Another;

For more details check the php documentation 有关更多详细信息,请查看php文档

It means the same everywhere not just CakePHP. 这意味着不仅仅是CakePHP。

You are importing a class from another namespace to yours. 您正在从另一个命名空间导入一个类到您的。

Without it you would have to use: 没有它你将不得不使用:

class ArticlesTable extends \Cake\ORM\Table

Instead of the shorter version (Table) you are using. 而不是您正在使用的较短版本(表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM