Jump to content

Referencing sub subjects and variables with a variable


binarymonkey

Recommended Posts

Hi,

 

Quick question, I am trying to do the following:

 

<?php

class SubObject {
  var $someVar = 'bar';
}

class MainObject {

  var $oSubObject;
  var $someVar='foo';

  function __construst() {
    $this->oSubObject = new SubObject();
  }

}

$oMainObject = new MainObject();

# works
$refString = 'someVar';
echo($oMainObject->{$refString}); #outputs 'foo'

# does not work
$refString = 'oSubObject->someVar';
echo($oMainObject->{$refString}); # fails (expected output 'bar')

?>

 

Where I am trying to get the sub object variable, I get the following E_NOTICE error:

 

Notice: Undefined property: MainObject::$oSubObject->someVar in test.php

 

Any ideas?

 

Matthew

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.