在类变量中嵌入变量

时间:2016-04-01 03:12:04

标签: php variables

我正在尝试创建一个可以为3个类别设置id,name和url变量的函数。我想使用一个功能完成所有这些

$this->set_variables('main,con,feat');

function set_variables($cats) {
    $cats = explode(',',$cats);
    foreach($cats as $cat) {
        $this->$cat.'_id' = $cat;
        $qry = mysqli_query($this->con,"SELECT categories_name,categories_url FROM categories WHERE categories_id=$this->$cat.'_id'");
        $row = mysqli_fetch_assoc($row);
        $this->$cat.'_name' = $row['categories_name'];
        $this->$cat.'_url' = $row['categories_url'];
    }
}

所以到最后,$ this-> main_id,$ this-> main_name,$ this-> main_url,$ this-> con_id,$ this-> con_name等...将...都是设定变量。但我正在努力寻找包含变量的方法,我已经厌倦了:

$this->$cat.'_id'
$this->[$cat]_id
$this->{$cat}_id

以及其他各种尝试,但似乎无法让它们中的任何一个设置。 那么如何设置这些变量?

0 个答案:

没有答案
相关问题