简体   繁体   English

如何声明php变量是常量

[英]How to declare a php variable to be constant

I have some closures that I have bound to some variables. 我有一些闭包,我已经绑定了一些变量。

eg 例如

$x = function() { echo "hi there"; };

I want to make sure that $x never gets inadvertently switched to some other value. 我想确保$ x永远不会无意中切换到其他值。 Any idea how to do this? 知道怎么做吗?

I can't use constants as they only resolve to scalar values. 我不能使用常量,因为它们只解析标量值。

What's the purpose of this code? 这段代码的目的是什么?

I mean, functions are constants by themselves, as they cannot be re-declared . 我的意思是,函数本身就是常量 ,因为它们不能被重新声明 Why not doing this instead then? 为什么不这样做呢?

function x() { echo "hi there"; };

If you're working in a closure, you can always use a namespace , so your function won't meet with a colliding one outside of the closure. 如果你在闭包中工作,你总是可以使用命名空间 ,所以你的函数不会遇到闭包之外的碰撞。

I Don't think you can achieve that as: 我不认为你能做到这一点:

  • Variables are meant to change their value 变量意味着改变它们的价值
  • Constants can't hold a function 常量不能保持功能

But I came with a workaround, is the only thing I came up with, dunno if it will be ok for you: Using Classes And it's __destruct() magic method 但是我找到了一个解决方法,这是我想出的唯一一件事,不管它是否适合你: 使用类和它的__destruct()魔术方法

Class Hulk {
    function __construct(){
        echo "Hulk is born!<br>";
    }

    function __destruct(){
        throw new Exception('Someone is trying to destroy Hulk, not knowing that he is indestructible!!!');
    }
}

$x = new Hulk();
$x = "hello";

When you try to assign "hello" to X it will throw an exception: 当您尝试将“hello”分配给X时,它将引发异常:

 Fatal error: Uncaught exception 'Exception' with message 'Someone is trying to destroy Hulk, not knowing that he is indestructible!!!' in /Applications/MAMP/htdocs/test.php:38 Stack trace: #0 /Applications/MAMP/htdocs/test.php(44): Hulk->__destruct() #1 {main} thrown in /Applications/MAMP/htdocs/test.php on line 38

You can also make it more silent doing just an echo or whatever you want. 你也可以通过回声或任何你想要的东西使它变得更加沉默。 Also this way you can group a serie of functions as methods inside the class, and ensure they will be always accesible. 这样,您可以将一系列函数分组为类中的方法,并确保它们始终可访问。

well good way to work with Closure is to wrap it with some helper class here is what i use 使用Closure的好方法是用一些帮助类包装它是我使用的

class Events {
    private $events;
    public function addEvents($eventName, Closure $c){
    $this->events[$eventName] = $c;
  }
   public function call($eventName, $args = array()){
    if (empty($args)){
        $this->events[$eventName]();
   }else {
     call_user_func_array($this->events[$eventName], $args);
     }
   }
}

usage 用法

$events = new Events();
$events->addEvents('event', function(){
    echo 'hi'; 
 });
$events->call('event');

here codepad test link 这里是键盘测试链接

Just write a regular named function? 只写一个常规的命名函数? If you want a constant fixed name for it, that would seem to be the obvious answer. 如果你想要一个固定的固定名称,这似乎是明显的答案。

If you absolutely have to have it dynamic as per your code, but not changeable from outside your control, I suggest putting it into a class. 如果你必须根据你的代码让它动态,但不能从你的控制之外改变它,我建议把它放到一个类中。 Have the actual variable set as to private, and provide a public getter for it, but not a setter. 将实际变量设置为private,并为其提供公共getter,但不提供setter。

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

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