Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thank you to all who responded. I was able to come up with a solution that works for what I need.</p> <p>Before I get there, as @PeeHaa stated, the __get and __set magic methods are the way to go here. However, to reach the stated goal of the original post, we require an object hierarchy. As far as I can tell, that's how SimpleXML is able to do what I described in my original post and subsequent edits, again alluded to by @PeeHaa in a comment. My original idea is indeed impossible [deep sigh of regret].</p> <p>Below is a very primitive view of what I'm going to do to accomplish this. I've done some pre-work and it appears to work as expected. Obviously, I am going to fill this out and refine it to fit my specific needs. It is also missing some sub-object creation code and sub-type intelligence in the interest of brevity.</p> <pre><code>class Foo { protected $_value; protected $children = array(); public function __construct($value) { $this-&gt;_value = $value; } public function setValue($value) { $this-&gt;_value = $value; } public function __toString() { return $this-&gt;_value; } public function __set($key, $value) { if(isset($this-&gt;children[$key]) == false) { $this-&gt;children[$key] = new self($value); } else { $this-&gt;children[$key]-&gt;setValue($value); } } public function __get($key) { return $this-&gt;children[$key]; } } $foo = new Foo(""); $foo-&gt;myVar = "some value"; // assigns "some value" to $foo-&gt;myValue-&gt;_value print_r($foo-&gt;myVar); // outputs that we have a Foo echo $foo-&gt;myVar; // outputs the contents of $foo-&gt;myValue-&gt;_value aka "some value" // This works and produces the string value of both "myVar" and "anotherVar", // with "anotherVar" being an instance of Foo. $foo-&gt;myVar-&gt;anotherVar = "some other value"; </code></pre> <p>Again, thank you all for your contributions and your patience as I worked this out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload