简体   繁体   English

处理PHP中常量的最佳方法

[英]Best way to deal with constants in PHP

Say I have a class with some methods that take a true/false value. 说我有一个带有一些采用true / false值的方法的类。 For example: 例如:

class MyClass
{
    public function hasSomething($value)
    {
        $something = $value;
    }
}

Where $something can be true/false. 其中$ something可以为true / false。 If I call that in code somewhere, it isn't very nice: 如果我在某处的代码中调用它,那就不是很好:

$MyClass->hasSomething(true);

It can be a bit hard from the method name and the value true to determine what the code is doing. 从方法名称和值为true可能很难确定代码在做什么。 So I like to use constants to make it nicer. 因此,我喜欢使用常量使其更美观。

I've been doing it like this: 我一直在这样做:

abstract class MY_CLASS_CONSTANTS
{
    const HAS_THING_ONE = true;
    const NO_THING_ONE = false;
}

Now when I call MyClass->hasSomething I can go MyClass->hasSomething(MY_CLASS_CONSTANTS::HAS_THING_ONE) which imo is far more expressive and should make the code easier to understand. 现在,当我调用MyClass->hasSomething我可以转到MyClass->hasSomething(MY_CLASS_CONSTANTS::HAS_THING_ONE) ,该imo更具表现力,应该使代码更易于理解。

I like the idea of putting the const in a class because it then becomes more obvious what those consts are supposed to be used with. 我喜欢将const放在类中的想法,因为这样一来,这些const应该与之一起使用就变得更加明显。

Is this a bad idea? 这是一个坏主意吗? Is there a better way to do it? 有更好的方法吗? How do other people deal with this? 其他人如何处理?

Why not creating a method, which would be ... less verbose and more explicit: 为什么不创建一个方法,它会变得更冗长,更明确:

MyClass->hasOneThing();
MyClass->hasNothing();

And in your class, you are free to delegate: 在课堂上,您可以自由委托:

class MyClass
{
    private function hasSomething($value) {$something = $value;}
    public function hasOneThing() {$this->hasSomething(true);}
    public function hasNothing() {$this->hasSomething(false);}
}

I don't use the constants; 我不使用常量; they are useless if that small case especially when the parameter's type is boolean. 如果这种情况很小,则它们是无用的,尤其是当参数的类型为布尔值时。

Beside, your method design is wrong. 此外,您的方法设计是错误的。 If its name is has or is , it should return something like a boolean, and not update the value. 如果其名称为hasis ,则应返回类似布尔值的内容,而不更新值。

It's good idea to use php class constants. 使用php类常量是个好主意。

I'll give some example that would make your code more understandable and readable, 我将举一些示例,使您的代码更易于理解和理解,

Example 1: (inside the class itself) 示例1 :( 在类本身内部)

class Exam
{
    /**
     * available grades
     */
    const GRADE_EXCELLENT   = 'A';
    const GRADE_VERY_GOOD   = 'B';
    const GRADE_MIN_PASS    = 'C';
    const GRADE_CLOSE_FAIL  = 'D';
    const GRADE_FAIL        = 'E';

    private $finalGrade;

    /**
     * one of const - Exam::GRADE_...
     * 
     * @param char $grade
     */
    public function setGrade($grade)
    {
        $this->finalGrade = $grade;
    }
}


$exam = new Exam();
$exam->setGrade(Exam::GRADE_VERY_GOOD);


Example 2: (having separate class) 示例2 :( 具有单独的类)

class Exam_Grades
{
    /**
     * available grades
     */
    const EXCELLENT   = 'A';
    const VERY_GOOD   = 'B';
    const MIN_PASS    = 'C';
    const CLOSE_FAIL  = 'D';
    const FAIL        = 'E';
}

$exam->setGrade(Exam_Grades::FAIL);


Example 3: (define constants) 示例3 :( 定义常量)

define('EXAM_GRADE_EXCELLENT',  'A');
define('EXAM_GRADE_VERY_GOOD',  'B');
define('EXAM_GRADE_MIN_PASS',   'C');
define('EXAM_GRADE_CLOSE_FAIL', 'D');
define('EXAM_GRADE_FAIL',       'E');

$exam->setGrade(EXAM_GRADE_MIN_PASS);

This way also code completion is available and is good practice to use constants. 这种方式也可以完成代码,这是使用常量的好习惯。

Most cases I prefer first example style, for me it does make much more sense, it's your choice, do whatever is better for you... 在大多数情况下,我更喜欢第一个示例样式,对我而言,这样做确实更有意义,这是您的选择,做对自己更好的事情...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM