Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的索引器_Php_Class - Fatal编程技术网

PHP中的索引器

PHP中的索引器,php,class,Php,Class,我一直在玩弄simplexml\u load\u file()函数,该函数返回simplexmlement,为了学习,我尝试了weather.gov restful服务带来一些数据,看看它是如何工作的 所以基本上我就是这么做的: $xml = simplexml_load_file("http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php?whichClient=NDFDge

我一直在玩弄
simplexml\u load\u file()
函数,该函数返回
simplexmlement
,为了学习,我尝试了weather.gov restful服务带来一些数据,看看它是如何工作的

所以基本上我就是这么做的:

$xml = simplexml_load_file("http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php?whichClient=NDFDgen&lat=38.99&lon=-77.01&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=&featureType=&requestedTime=&startTime=&endTime=&compType=&propertyName=&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2015-06-10T00%3A00%3A00&maxt=maxt&Submit=Submit");

var_dump($xml);
该链接属于
weather.gov
和rest服务

现在我的问题来了:

当我转储变量时,有什么东西引起了我的注意,我找不到一种方法来向自己描述它

下面是转储变量的一部分:

object(SimpleXMLElement)[1]
  public '@attributes' => 
    array
      'version' => string '1.0' (length=3)
  public 'head' => 
    object(SimpleXMLElement)[2]
      public 'product' => 
        object(SimpleXMLElement)[4]
          public '@attributes' => 
            array
              ...
当我看到public
'@attributes'=>array
时,我以为我可以输入变量
$xml->attributes['version']
,但我错了,因为我知道这是正确的方法
$xml['version']
我开始想,既然
$xml
在这里变成了数组,这是怎么可能的,但是在运行第一个代码
$xml->head->product->title
后,这段代码也可以正常工作,它返回
head>product
下定义的title元素的值

它看起来像C#中的索引器。基本上,这是下面c#中的有效代码:

但是我想不出一种在PHP类中提供相同功能的方法


有人能告诉我在PHP(一个索引器)中如何调用“
@attributes”
?以及如何在PHP中实现此功能。

代码>@是一个Xpath字符。XPath由SimpleXML调用,如下所示:
$xml->XPath()
。例如,如果您想获取元素
product
的属性
srsName
,它看起来会像这样(这个例子显然不准确,您需要找到更多关于xpath的信息):

$xml->xpath("product/@srsName");

可以使用索引器访问从和/或实现继承的类的对象(其
ArrayObject
本身实现)。尽管
SimpleXMLElement
本身并没有实现任何一个(它可能是一个内部实现),但您可以使用它们在自己的类中实现相同的功能。

查找
ArrayObject
类和
ArrayAccess
接口。而不是编写注释,把它写下来作为答案,这样我就可以把它标记为答案了……对吗???我现在已经把它作为了答案:)
$xml->xpath("product/@srsName");