常量类与全局常量 [英] Constants class vs global constants

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

问题描述

例如,我正在一个项目中,所有全局常量都在称为常量的类中定义。

I'm working on a project where all the global constants have been defined in a class called Constants, for example

class Constants
{

  const MIN_VALUE = 0.0;
  const MAX_VALUE = 1.0;

  public static function getMinValue()
  {
    return self::MIN_VALUE;
  }

  public static function getMaxValue()
  {
    return self::MAX_VALUE;
  }

  public static function getValueDependingOnURL()
  {
    if($_SERVER['REQUEST_URI'] == 'something')
    {
      return self::MIN_VALUE;
    }
    else
    {
      return self::MAX_VALUE;
    }
  }
}

然后在整个代码中 Constants :: getMaxValue()用于获取常量的值。这似乎是一种非常奇怪的方法,为什么您不只在最外层的范围内使用 define 函数呢?我知道 define()可能很慢,但是肯定必须调用类属性也不是最有效的方法吗?

Then throughout the code something like Constants::getMaxValue() is used to get the value of a constant. This seems a very strange approach why wouldn't you just use the define function in the outermost scope? I know define() can be quite slow but surely having to call a class property is not the most efficient way either?

编辑:某些函数中也有条件,因此为什么要调用函数

Also some of the functions have conditions in them hence why functions are called

推荐答案

弄清问题:与全局 define() ed配置常量相比,配置容器的优势是什么?

Clearify the question: What is the advantage of a configuration container over globally define()ed configuration contstants?

这些优点是OOP提供的所有优点:数据抽象和封装继承多态更好的设计模式集成 >。

The advantages are all the advantages that OOP offers: Data Abstraction and Encapsulation, Inheritance, Polymorphism and better design pattern integration.

该主题的读者似乎有些困惑,只专注于您的课堂而不是主要问题。为了澄清这一点,让我举一个例子:

The readers of this thread seem a little confused and focus on your class rather than the main question. To also clearify this, let me give you an example:

class Configuration
{
    protected $someValue;
}

class ConfigurationDev extends Configuration
{
    protected $baseUrl = 'http://devel.yoursite.com/';
}

class ConfigurationLive extends Configuration
{
    protected $baseUrl = 'http://www.yoursite.com/';
}

index.php:

index.php:

<?php
$config   = new ConfigurationDev;
$tracking = new Tracking($config);
...

类跟踪:

class Tracking
{
    public function __construct(\Configuration $config) {
        if ($config instanceof \ConfigurationLive) {
            // We are in live environment, do track
        } else {
            // Debug Notice: We are NOT in live environment, do NOT track
        }
    }
}

场景说明:

想象您要跟踪用户,但仅在实时系统上,而不在开发系统上。跟踪类希望使用实时配置,但如果不是实时配置,则会中止(不产生影响)。

Imagine you want to track users, but only on the live system, not on your development system. The Tracking class expects a live configuration but aborts (without impact) if its not the live config.

您的类具有 const 并不是最好的,因为 const 表示您不想更改变量。请勿将变量用于可能更改的值。您也不应使用静态内容,因为它通常与依赖项注入冲突。传递真实对象!

Your class with const is not the best, because const implies you do not want to change the variables. Do not use the variable for values that may change. You shouldn't use static stuff either because it mostly conflicts with dependency injection. Pass real objects!

您的函数公共静态函数getValueDependingOnURL()应该放在Helper类中,而不是

Your function public static function getValueDependingOnURL() should be placed in a Helper class, not in a Constant/Configuration container either.

class Helper
{
    protected $config;

    public function __construct(\Configuration $config) {
        $this->config = $config;
        return $this;
    }

    public function getValueByUrl($url) {
        if ($url == 'something') {
            return $config->getMinValue();
        } else {
            return $config->getMaxValue();
        }
    }
}

现在您可以拥有不同的集合了助手类所依赖的配置:

Now you can have different sets of configuration which the helper class relies on:

$config = new ConfigurationLive;
$helper = new Helper($config);
$value  = $helper->getValueByUrl($_SERVER['REQUEST_URI']);






有很多最佳实践设计模式的东西,代码风格和OOP(在我的示例中),对它们进行了解,您将获得比问题读者更高的软件工程水平。祝你好运!


There is a lot of best practice design pattern stuff, code style and OOP in my examples, learn about those and you will gain a much higher level of software engineering than the readers of your question. Good luck!

这篇关于常量类与全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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