PHP: Dynamic Object Names

Jonas Ballestad picture Jonas Ballestad · Nov 23, 2012 · Viewed 11.6k times · Source

Possible Duplicate:
Get PHP class property by string

This is my original code:

function generateQuery($type, $language, $options)
{
    // Base type
    $query = $this->Queryparts->print['filter'];

    // Language modifiers
    // Additional options

    return $query;
}

The "print" is an array/hash defined as an object (with "(object)" casting). I wish to do something like this:

    $query = $this->Queryparts->$type['filter'];

To use the the $type variable as the object name. Is this possible?

Answer

som picture som · Nov 23, 2012
$query = $this->Queryparts->{$type}['filter'];