PHP常量:优点/缺点 [英] PHP Constants: Advantages/Disadvantages

查看:485
本文介绍了PHP常量:优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直习惯将整数值赋给常量,只是使用常量名作为识别其目的的方法。但是,在某些情况下,这需要在需要字符串表示时编写类似于typeToString($ const)的函数。显然这是低效率和不必要的,但只是一个问题每一次,一会儿。



所以我的问题是,是否有任何其他权衡,我应该考虑?哪种情况被认为是更清洁/更符合标准?

情况1 :(不需要字符串版本时更快?)

  class Foo {
const USER_TYPE_ADMIN = 0;
const USER_TYPE_USER = 1;
const USER_TYPE_GUEST = 2;

public $ userType = self :: USER_TYPE_ADMIN;

public function __construct($ type){
$ this-> userType = $ type;
}

public function typeToString(){
switch($ this-> userType){
case self :: USER_TYPE_ADMIN:
return'admin ';
break;

case self :: USER_TYPE_USER:
return'user';
break;

case self :: USER_TYPE_GUEST:
return'guest';
break;

默认值:
return'unknown';
break;
}
}
}

$ foo = new Foo(Foo :: USER_TYPE_GUEST);
echo $ foo-> typeToString();
//显示guest

案例2:需要字符串版本)

  class Foo {
const USER_TYPE_ADMIN ='admin';
const USER_TYPE_USER ='user';
const USER_TYPE_GUEST ='guest';

public $ userType = self :: USER_TYPE_ADMIN;

public function __construct($ type){
$ this-> userType = $ type;
}
}

$ foo = new Foo(Foo :: USER_TYPE_GUEST);
echo $ foo-> userType();
//显示guest


解决方案

性能差异将是可以忽略不计,除非你存储了很多。
我会更简洁地写 toString()方法:

  $ strings = array 

self :: USER_TYPE_ADMIN =>'admin',
self :: USER_TYPE_USER =>'user',
);

if(!isset($ strings [$ type]))
return'unknown';

return $ strings [$ type];

此外,您可以使 $ strings 数组a static


Lately I've been in the habit of assigning integer values to constants and simply using the constant name as a means of identifying its purpose. However, in some cases this has resulted in the need to write a function like typeToString($const) when a string representation is needed. Obviously this is inefficient and unneccesary, but is only an issue every once and a while.

So my question is, are there any other tradeoffs I should consider? Which case is considered to be cleaner/more standards-compliant? Also, is the performance difference negligable for most cases?

Case 1: (faster when a string version is not needed?)

class Foo {
    const USER_TYPE_ADMIN = 0;
    const USER_TYPE_USER = 1;
    const USER_TYPE_GUEST = 2;

    public $userType = self::USER_TYPE_ADMIN;

    public function __construct($type) {
        $this->userType = $type;
    }

    public function typeToString() {
        switch($this->userType) {
            case self::USER_TYPE_ADMIN:
                return 'admin';
                break;

            case self::USER_TYPE_USER:
                return 'user';
                break;

            case self::USER_TYPE_GUEST:
                return 'guest';
                break;

            default:
                return 'unknown';
                break;
        }
    }
}

$foo = new Foo(Foo::USER_TYPE_GUEST);
echo $foo->typeToString();
// Displays "guest"

Case 2:(faster/easier when a string version is needed)

class Foo {
    const USER_TYPE_ADMIN = 'admin';
    const USER_TYPE_USER = 'user';
    const USER_TYPE_GUEST = 'guest';

    public $userType = self::USER_TYPE_ADMIN;

    public function __construct($type) {
        $this->userType = $type;
    }
}

$foo = new Foo(Foo::USER_TYPE_GUEST);
echo $foo->userType();
// Displays "guest"

解决方案

The performance difference will be negligible unless you're storing a lot of them. I'd write the toString() method more concisely:

$strings = array
(
    self::USER_TYPE_ADMIN => 'admin',
    self::USER_TYPE_USER => 'user',
);

if (!isset($strings[$type]))
    return 'unknown';

return $strings[$type];

Also, you could make the $strings array a static.

这篇关于PHP常量:优点/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆