如何在 php 文件中添加常量


How to add constant in to php file

如何在php文件中添加常量?我需要在所有填充中使用链接,我需要如何以及在何处添加它?我需要使用这一行:

$merchantApiUrl = '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;