PHP code example of huangdijia / constants

1. Go to this page and download the library: Download huangdijia/constants library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

huangdijia / constants example snippets


class ErrorCode
{
    const NOT_FOUND = 404;
    const SERVER_ERROR = 500;

    public static $errors = [
        self::NOT_FOUND => 'Not Found',
        self::SERVER_ERROR => 'Server Error',
    ];

    public static function getMessage($code)
    {
        return self::$errors[$code] ?? '';
    }
}

namespace App\Constants;

use Huangdijia\Constants\AbstractConstants;

/**
 * @method static string getMessage(string $code)
 * @method static string getMessageCn(string $code)
 * @method static string getMessageEn(string $code)
 */
class ErrorCode extends AbstractConstants
{
    /**
     * @Message("%s not found!")
     */
    const NOT_FOUND = 404;

    /**
     * @Message("Server Error")
     */
    const SERVER_ERROR = 500;

    /**
     * @MessageCn("成功")
     * @MessageEn("Success")
     */
    const OK = 1;

    /**
     * @MessageCn("失败")
     * @MessageEn("Failure")
     */
    const FAILURE = 0;
}

var_dump(ErrorCode::getMessage(ErrorCode::SERVER_ERROR)); // Server Error
var_dump(ErrorCode::getMessage(ErrorCode::NOT_FOUND, '/api')); // /api not found!
var_dump(ErrorCode::getMessageCn(ErrorCode::OK)); // 成功
var_dump(ErrorCode::getMessageEn(ErrorCode::OK)); // Success