php 中的 require 和 use 是否相似?
Whether require and use are similiar in php?
使用RingCentral\SDK;
或
require('/home/developer/workspace/ringcentral/demo/SDK.php');
与其使用 'use' - php 5.6 命令,不如使用 require - php 5.3 ?
SDK class 文件包含以下代码
class SDK
{
const VERSION = '0.5.0';
/** @var Platform */
protected $platform;
/** @var Context */
protected $context;
public function __construct($appKey, $appSecret, $server)
{
$this->context = new Context();
$this->platform = new Platform($this->context, $appKey, $appSecret, $server);
}
public function getPlatform()
{
return $this->platform;
}
public function getSubscription()
{
return new Subscription($this->context, $this->platform);
}
public function getContext()
{
return $this->context;
}
}
require()
用于请求另一个文件,而 use
用于使用来自另一个命名空间的 class,因此这两个命令完全不同且不可比较。
在您的情况下,您将需要使用 require()
,除非您正在使用任何类型的自动加载,例如 composer
以使 class 在您的文件中可用。
您必须需要 一个自动加载器以允许 SPL 自动加载来解析使用 语句:
// if you use Composer
require('vendor/autoload.php');
// or just require a PHAR
require('path-to-sdk/ringcentral.phar');
use RingCentral\SDK\SDK;
$sdk = new SDK(...);
您实际上可以省略 use 语句并使用完全限定的 class 名称:
$sdk = new RingCentral\SDK\SDK(...);
使用RingCentral\SDK;
或
require('/home/developer/workspace/ringcentral/demo/SDK.php');
与其使用 'use' - php 5.6 命令,不如使用 require - php 5.3 ?
SDK class 文件包含以下代码
class SDK
{
const VERSION = '0.5.0';
/** @var Platform */
protected $platform;
/** @var Context */
protected $context;
public function __construct($appKey, $appSecret, $server)
{
$this->context = new Context();
$this->platform = new Platform($this->context, $appKey, $appSecret, $server);
}
public function getPlatform()
{
return $this->platform;
}
public function getSubscription()
{
return new Subscription($this->context, $this->platform);
}
public function getContext()
{
return $this->context;
}
}
require()
用于请求另一个文件,而 use
用于使用来自另一个命名空间的 class,因此这两个命令完全不同且不可比较。
在您的情况下,您将需要使用 require()
,除非您正在使用任何类型的自动加载,例如 composer
以使 class 在您的文件中可用。
您必须需要 一个自动加载器以允许 SPL 自动加载来解析使用 语句:
// if you use Composer
require('vendor/autoload.php');
// or just require a PHAR
require('path-to-sdk/ringcentral.phar');
use RingCentral\SDK\SDK;
$sdk = new SDK(...);
您实际上可以省略 use 语句并使用完全限定的 class 名称:
$sdk = new RingCentral\SDK\SDK(...);