donglengyuan6826 2014-06-18 11:51
浏览 22

有没有办法在String中转换常量或定义的名称? [PHP]

I have a function in PHP that I must pass a string parameter into. I have a file with many constant definitions, like:

define('MSG_CATEGORY_THINGS', things);

I would like to transform my constant MSG_CATEGORY_THINGS into a string with value 'MSG_CATEGORY_THINGS' without putting single quotes in the parameter. Is it possible to do that?

  • 写回答

1条回答 默认 最新

  • dph23577 2014-06-18 11:57
    关注

    You could recieve all your constants by:

    $constants = get_defined_constants(true);
    $myConstants = $constants["user"];
    

    This will return an associative array, i.e.

    ['MSG_CATEGORY_THINGS'=>things]
    

    If you want it as strings you could do:

    extract($myConstants)
    

    You will now have a variable called $MSG_CATEGORY_THINGS with a value of things :)

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了