如何在YII2中逐步使用外部类

时间:2019-01-19 17:10:55

标签: php yii2

所以我有一个班级,我曾两次在其他非YII项目中使用过,但现在我想在YII2项目中使用相同的班级。我已经进行了一些搜索,但是我一直陷于困境。以下是我到目前为止所做的事情:

我在供应商目录中创建了一个名为“实用程序”的文件夹,该实用程序文件夹包含我的名为“ AT_Response.class.php”的类。所以我的问题是如何在模型或控制器中包含或调用此类。

我检查了一些链接,如:

https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-integration

https://forum.yiiframework.com/t/not-understanding-how-to-use-external-php-library/79679

类代码:

<?php

class AT_Response {

    static private $response = array
        (
        '9999' => array('description' => "Unexpected Response", 'definite' => true, 'status' => "Indeterminate"),
        '00' => array('description' => "Success", 'definite' => true, 'status' => "Success"),
        'NNC_AUTH_01' => array('description' => /*"Status unknown, please wait for settlement report"*/"System Error", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_01' => array('description' => "Ttimed out", 'definite' => false, 'status' => "Indeterminate"),
        'NNC_VTU_02' => array('description' => "Exceeded max number of requests for Phone number per time period", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_03' => array('description' => "Invalid target MSISDN supplied", 'definite' => true, 'status' => "Failure"),
        '-1' => array('description' => "Not successful", 'definite' => false, 'status' => "Failure"),
    );

    static function getResponseByCode($respCode) {
        if (isset(self::$response[$respCode]))
            return self::$response[$respCode];

        //else
        return self::$response['9999'];
    }

}

谢谢

1 个答案:

答案 0 :(得分:0)

进行一些更改,您可以将任何自定义类用作帮助程序组件。您需要对现有的类使用namespaceuse语句,请参见下文

<?php
namespace app\components;

class Response
{

    /**
     * @var array
     */
    private static $response = array
        (
        '9999' => array('description' => "Unexpected Response", 'definite' => true, 'status' => "Indeterminate"),
        '00' => array('description' => "Success", 'definite' => true, 'status' => "Success"),
        'NNC_AUTH_01' => array('description' => /*"Status unknown, please wait for settlement report"*/"System Error", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_01' => array('description' => "Ttimed out", 'definite' => false, 'status' => "Indeterminate"),
        'NNC_VTU_02' => array('description' => "Exceeded max number of requests for Phone number per time period", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_03' => array('description' => "Invalid target MSISDN supplied", 'definite' => true, 'status' => "Failure"),
        '-1' => array('description' => "Not successful", 'definite' => false, 'status' => "Failure")
    );

    /**
     * @param $respCode
     */
    public static function getResponseByCode($respCode)
    {
        if (isset(self::$response[$respCode])) {
            return self::$response[$respCode];
        }

        return self::$response['9999'];
    }

}

将上述类保存在Response.php文件夹中名为app\components的文件basic-app中,如果使用的是common\components,则保存在advanced-app中,但不要忘记进行更改代码中的namespace

然后您可以像getResponseByCode()app\components\Response::getResponseByCode($responseCode)那样调用函数common\components\Response::getResponseByCode($responseCode)