如何在php文件中添加常量

如何在php文件中添加常量,php,constants,Php,Constants,如何在php文件中添加常量?我需要使用链接在所有填写方式和我需要添加它的地方? 我需要用这句话: $merchantApiUrl='1'https://spectrocoin.com/api/merchant/1“ 最好的地方在哪里 <?php require_once DIR_SYSTEM . 'library/spectrocoin/SCMerchantClient.php'; class ControllerPaymentSpectrocoin extends Controller

如何在php文件中添加常量?我需要使用链接在所有填写方式和我需要添加它的地方? 我需要用这句话:

$merchantApiUrl='1'https://spectrocoin.com/api/merchant/1“

最好的地方在哪里

<?php
require_once DIR_SYSTEM . 'library/spectrocoin/SCMerchantClient.php';
class ControllerPaymentSpectrocoin extends Controller
{
    var $time = 600;
    public function index()
    {
.
.
.
}
您可以使用

define('CONST', 'http://...');
可以在任何地方使用,比如

echo CONST;
或者在某个类中创建一个在所有文件中都可用的常量。例如:

class SCMerchantClient {
   const URL = 'http://...';

}
然后在控制器中

public function index()
{
   echo SCMerchantClient::URL;
}

您可以在配置文件中定义常量,如果您有或位于该文件的顶部

使用
define
方法定义常数,如下所示:

define('MERCHANTAPIURL', 'https://spectrocoin.com/api/merchant/1');
然后在代码中,您可以这样使用它:

MERCHANTAPIURL

我认为最好的地方是配置文件

如果此链接已更改,则无需更改代码。

SCMerchantClient.php
如下定义常量:-

define('MERCHANT_API_URL', 'spectrocoin.com/api/merchant/1')
如果您想在ControllerPaymentSpectrocoin中使用它,请编写如下内容:-

echo MERCHANT_API_URL;
定义('MERCHANT_API_URL','),并像使用MERCHANT_API_URL一样使用它