Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:41:55+00:00 2026-06-11T13:41:55+00:00

Here’s what I want to do: $var = new ObjectICreated(yay); echo $var; // outputs

  • 0

Here’s what I want to do:

$var = new ObjectICreated("yay");
echo $var; // outputs "yay" via the __toString() magic method
$var = "Boo"; // $var is still a ObjectICreated, but will now output "Boo" from __toString()

Am I crazy? I think SimpleXML does this very thing, but I’m not sure how. Any thoughts?

Reasoning: I want to track changes to a particular object without having to use billions of getters/setters.

Ok, thanks for the comments, for posterity. SimpleXML DOES do this. The following works based on code from http://www.php.net/manual/en/simplexml.examples-basic.php example #9.

$x = simplexml_load_string($xml); // xml from example #9
// Pre-reference value
print_r($x->movie[0]->characters->character[0]->name);  
// Assign to reference of a SimpleXMLElement
$x->movie[0]->characters->character[0]->name = 'Miss Coder';
print_r($x->movie[0]->characters->character[0]->name);

The output is as follows:

SimpleXMLElement Object ( [0] => Ms Coder ) 
SimpleXMLElement Object ( [0] => Miss Coder )

As you can see, it is still a SimpleXMLElement, as it was prior to the assignment of “Miss Coder”.

Thank you again everyone for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:41:56+00:00Added an answer on June 11, 2026 at 1:41 pm

    Thank you to all who responded. I was able to come up with a solution that works for what I need.

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

    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.

    class Foo { 
        protected $_value;
        protected $children = array();
        public function __construct($value) {
            $this->_value = $value;
        }
        public function setValue($value) {
            $this->_value = $value;
        }
        public function __toString() {
            return $this->_value;
        }
        public function __set($key, $value) {
            if(isset($this->children[$key]) == false) {
                $this->children[$key] = new self($value);
            } else {
                $this->children[$key]->setValue($value);
            }
        }
        public function __get($key) {
            return $this->children[$key];
        }
    }
    
    $foo = new Foo("");
    $foo->myVar = "some value"; // assigns "some value" to $foo->myValue->_value
    print_r($foo->myVar); // outputs that we have a Foo
    echo $foo->myVar; // outputs the contents of $foo->myValue->_value aka "some value"
    
    // This works and produces the string value of both "myVar" and "anotherVar",
    // with "anotherVar" being an instance of Foo.
    $foo->myVar->anotherVar = "some other value";
    

    Again, thank you all for your contributions and your patience as I worked this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is what I am currently doing. PHP echo's out the recent post in
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
here is how i call the following function: List<decimal> numbers = new List<decimal>(); numbers.Add(210m);
Here is my code...I have two dimensional matrices A,B. I want to develop the
here i want to understand the architecture of bluez (Bluetooth Stack Protocol). I understood
here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.