Amaç PHP ve anahtar değeri kodlama

0 Cevap php

Benim kod kısmını ben böyle bir şey gerekiyor:

$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.

Bunu kolayca yapabilecekleri zaman, böyle bir şey diziler için çalıştığını biliyorum:

$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

0 Cevap