开发者

dynamic object creation php, creating object from param problem

开发者 https://www.devze.com 2023-01-12 01:01 出处:网络
So Im trying to make an object dynamically, please see below code from my item model function get_total($param)

So Im trying to make an object dynamically, please see below code from my item model

function get_total($param)
{

    $i = new Item();
    $items = $i->get();
    $total_value = 0;
    $query = "$item->".$param;
    foreach($items as $item)
    {
        if($item->qty > 0)
        {
            $total_value += $query * $item->qty;
        }
    }
    return $total_value;
}

So for instance, in my mysql table, I have cost and price mapped as properties of the $item object -- So for example Im trying to make that variable $query, output the following obj开发者_StackOverflowect -- $item->cost

So that I can call the function from my view like:

  echo $i->get_total('price');

or

  echo $i->get_total('cost');

To make my models selection more dynamic. But Im not sure the proper way to do it, because it seems that trying to create a dynamic object by concating the value for it based on the param, is a no no. So can someone please show me how to properly do it?

Thanks in advance for help!

So that on my


Just replace:

$query = "$item->".$param;

by

$query = $item->$param;

Hope that helps=)

0

精彩评论

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