如何访问带有@符号的PHP对象属性


How do I access a PHP object attribute having a @ sign?

我的问题和这个类似

我如何访问PHP对象属性有美元符号?

但是我用@ (at)符号代替了美元。

对象是:

object(SimpleXMLElement)#8 (1) { 
  ["@attributes"]=> array(3) { 
  ["type"]=> string(9) "image/png" 
  ["href"]=> string(62) "http://someurl.com/images/193/image_normal.jpg" 
  ["rel"]=> string(5) "image" 
  } 
}

和我必须访问@attributes变量(实际上是href组件),但是PHP不允许这样的语法:

$object->@attributes

在引用的资源之后,我尝试了以下方法:

$object->{'@attributes'};

$myvar = '@attributes';
$object->$myvar;

,但是这两种形式都不能访问变量。它打印:

object(SimpleXMLElement)#8 (0) {}

而我希望是一个向量

任何想法?由于

正常情况下$object->{'@attributeName'} - to ask your question.

但是,您想要使用SimpleXMLElement获得xml元素的属性,这是这样做的:

$attributeValue = (string) $xmlElement['attributeName'];

更多的用法:http://www.php.net/manual/en/simplexml.examples-basic.php

它是attributes参见SimpleXMLElement::attributes获取更多信息

例子
foreach($xml->attributes() as $a => $b) {
    echo $a,'="',$b,"'"'n";
}