dongpang1232 2012-09-12 22:26
浏览 42
已采纳

你可以通过将值赋给它的引用[关闭]来为PHP对象注入一个值吗?

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.

  • 写回答

3条回答 默认 最新

  • duanpiangeng8958 2012-09-13 02:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘