Is there a workaround for PHP's inability to use a function return value in write context?
<p><strong>I left my original question as is but wanted to point out the supposition of my question is flawed. Read the update for more</strong></p>

<hr>

<p>I was surprised to discover that this does not work in PHP:</p>

<pre><code>$someObjects = array();

function getInstance( $key )
{
    if ( ! $someObjects[$key] )
       $someObjects[$key] = new Object;

    return $someObjects[$key];
}

getInstance( 'mykey' )-&gt;property = 33;
</code></pre>

<p>The final line creates a fatal error:</p>

<pre><code>PHP Fatal error:  Can't use function return value in write context
</code></pre>

<p>It's a pattern that I've occasionally used in Actionscript that comes in handy when you need to define a bunch of objects explicitly (say in a config file) and you want to skip the instantiation step but also don't want to deal with the clutter of a bunch of nested arrays.</p>

<p>Any suggestions?</p>

<hr>

<p><strong>Update</strong></p>

<p>This is one of those <em>duh</em> moments after reading the responses. My question wrongly supposes that <code>getInstance( 'mykey' )-&gt;property = 33</code> is the root cause of the fatal error.</p>

<p>My example code was sloppy, and had two big problems (ooops):</p>

<ol>
<li>did not declare the <code>global</code> in the function body</li>
<li>incorrectly used <code>! $array[$key]</code> instead of <code>empty($array[$key])</code></li>
</ol>

<p>These problems were also in my project code but they do not appear to be the sole reason for the fatal error and I have since changed the project code and now I can't figure out what the other factor(s) are (?). At any rate, I got straightened out.</p>

<p>After the suggestions below this is the syntax I was aiming for:</p>

<pre><code>class MetaInfo
{
    public $title;
    public $description;
}

/**
 * @param $key string
 * @return MetaInfo
 */
function getMeta( $key )
{
    global $meta;

    if ( empty($meta[$key]) )
        $meta[$key] = new MetaInfo;

    return $meta[$key];
}

getMeta( 'path/to/page' )-&gt;title = "Double-check your work before posting";
</code></pre>
