【问题标题】:php: constant variable attach with stdclassphp:常量变量附加到 stdclass
【发布时间】:2011-06-08 10:49:17
【问题描述】:

是否可以通过像这样附加一个常量变量来调用数据库中的数据?

$table_result->description_{constant_varible};

所以我打算调用的实际stdclass是$table_result->description_B;return '34'

谢谢

【问题讨论】:

  • 您应该描述您的问题以获得更好的答案。现在,问题是接受'Yes''NO' 作为答案

标签: php variables join constants stdclass


【解决方案1】:

是的,这是可能的。例如。通过$obj->{expr}

<?php
$v = 'B'; // or a constant, doesn't matter
$table_result = foo();
echo $table_result->{'description_'.$v};

function foo() {
    $x = new StdClass;
    $x->description_B = 34;
    return $x;
}

【讨论】:

    【解决方案2】:

    您的解决方案应该有效(不确定)。 这是一个替代品。

    $varName =  'description_'.constant_varible;
    $table_result->$varName;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 2018-06-27
      • 2012-04-08
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多