简体   繁体   English

是否可以在定义的常量中包含一个函数?

[英]is it possible to have a function inside a defined constant?

According to https://www.tutorialspoint.com/php/php_constants.htm a normal costant is define by using define() function plus a value: 根据https://www.tutorialspoint.com/php/php_constants.htm,通过使用define()函数和一个值来定义正常的成本:

define('TEST', 123);

I think it would be interesting and useful if there were a way to define a constant with a function inside: 我认为,如果有一种方法可以定义一个内部带有函数的常量,那将是有趣而有用的:

define ('ME', function($test){
   if($test == true){
       return "Whats up";
   }else{
       return "whats down";
   }
}); 

Then perhaps I could do something like this? 那也许我可以做这样的事情吗?

 if($I_Said === ME($val)){
     do something;
 }

Is there a such a way to do this? 有这种方法吗?

There's no syntactical or other important difference on the caller side to: 调用方在以下方面没有语法或其他重要区别:

function ME() {}

if ($I_Said === ME($val))

So, just do that. 所以,就那样做。

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

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