开发者

php: constant variable attach with stdclass

开发者 https://www.devze.com 2023-03-11 23:44 出处:网络
Is it possible to call a data from database by attaching a constant variable like this? $table_result->description_{constant_varible};

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

精彩评论

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

关注公众号