php:用stdclass附加常量变量

时间:2011-06-08 10:49:17

标签: php variables join constants stdclass

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

$table_result->description_{constant_varible};

因此我打算调用的实际stdclass为$table_result->description_B; return '34';

由于

2 个答案:

答案 0 :(得分:0)

您的解决方案应该有效(不确定)。 这是另一种选择。

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

答案 1 :(得分:0)

是的,这是可能的。例如。通过$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;
}