Objective PHP and key value coding

Posted by Lukasz on Stack Overflow See other posts from Stack Overflow or by Lukasz
Published on 2010-06-05T19:53:33Z Indexed on 2010/06/05 20:02 UTC
Read the original article Hit count: 355

Filed under:
|
|
|
|

Hi Guys. In some part of my code I need something like this:

$product_type = $product->type;
$price_field = 'field_'.$product_type.'_price';
$price = $product->$$price_field;

In other words I need kind of KVC - means get object field by field name produced at the runtime. I simply need to extend some existing system and keep field naming convention so do not advice me to change field names instead.

I know something like this works for arrays, when you could easily do that by:

$price = $product[$price_field_key].

So I can produce key for array dynamically. But how to do that for objects? Please help me as google gives me river of results for arrays, etc... Thank you

© Stack Overflow or respective owner

Related posts about php

Related posts about value