开发者

php: constant variable attach with stdclass

Is it possible to call a data from database by attaching a constant variable like this?

$table_result->description_{constant_varible};

So that the actual stdclass I intend to call is $table_resu开发者_运维知识库lt->description_B; return '34';

Thanks


Yes, it's possible. E.g. via $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;
}


Your solution should work (not sure). Here is an alternate.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜