ZF2-10: iterating over a Zend_Config
Description
There are two xml-files.
<?xml version="1.0" encoding="utf-8" ?>
and
<?xml version="1.0" encoding="utf-8" ?>
and the sample code:
<?php
require_once 'Zend/Config.php';
require_once 'Zend/Config/Xml.php';
$config = new Zend_config_xml('/path/to/config.xml');
foreach ($config->files->file as $value)
echo 'id: '.$value->id.'
location: '.
htmlspecialchars($value->location).'';
?>
If the {{/path/to/config.xml}} to substitute {{config1.xml}}, then everything works correctly. But if you substitute {{config2.xml}} error
Notice: Trying to get property of non-object in D:\www\index.php on line 7
How can I iterate all items {{file}}, if their number is not known in advance (one or more than one)?
Comments
Posted by Rob Allen (rob) on 2009-04-05T12:04:41.000+0000
Can't fix at Zend_Config level. Workaround is to use UTF8 files without a BOM marker.
Posted by Rob Allen (rob) on 2009-04-05T12:12:32.000+0000
(Closed wrong issue!)
Posted by Rob Allen (rob) on 2009-04-05T12:19:40.000+0000
I'm not sure there's any way to resolve this issue due to the other than creating a dummy key to force an array:
might work, but don't forget to test for id being null.
Posted by Patrick van Dissel (tdm) on 2009-09-03T00:56:22.000+0000
This issue also exists when using the toArray() method of Zend_Config. The problem is that you just want the array to ALWAYS have the same layout. Config files tent to grow and be extended, just like XML files.
In my opinion the Zend_Config API should work the same as the of SimpleXml, here two extended examples and their output: h2. Using SimpleXML
Files with single file element, looping files->file: object(SimpleXMLElement)#3 (2) { ["id"]=> string(5) "index" ["location"]=> string(10) "index.html" } id: index localtion: index.html Files with single file element, looping files->file->id: object(SimpleXMLElement)#2 (1) { [0]=> string(5) "index" } id: index Files with multiple file elements, looping files->file: object(SimpleXMLElement)#6 (2) { ["id"]=> string(5) "index" ["location"]=> string(10) "index.html" } id: index localtion: index.html object(SimpleXMLElement)#3 (2) { ["id"]=> string(6) "index2" ["location"]=> string(11) "index2.html" } id: index2 localtion: index2.html Files with multiple file elements, looping files->file->id: object(SimpleXMLElement)#4 (1) { [0]=> string(5) "index" } id: indexh2. Using Zend_Config_Xml possibly a global Zend_Config or other Zend_Config* types issue, I have not tested that_
{color:red}note:{color} Output rendered with PHP in error_reporting mode = E_ALL | E_STRICT
Posted by Rob Allen (rob) on 2009-09-04T09:31:17.000+0000
I'm open to ideas on how to solve this without breaking backwards compatibility.
Posted by Ben Scholzen (dasprid) on 2009-09-04T10:48:51.000+0000
You could just do:
And then just work with the $files variable as array. That works pretty well.
Posted by Wim Godden (wimg) on 2011-04-28T14:25:29.000+0000
Although this is fixable, fixing it will always break backwards compatibility. When a developer knows there's only 1 item, code such as this will be broken :
Because it will in fact have to become :
I would advise against modifying this for ZF 1.x - not sure how this will be handled in ZF 2, but introducing this backwards incompatibility is not a good idea in any case...
The only other option is to modify just the magic/fluent notation, but that will make things complicated and will cause inconsistency between fluent notation and array notation, which should be avoided.
Posted by Rob Allen (rob) on 2011-04-28T14:47:51.000+0000
One idea I have had is to introduce a children() method that always returns an iterator/array regardless of whether there is is one or many children:
Thoughts on this approach?
Posted by Rob Allen (rob) on 2011-05-27T02:30:37.000+0000
Moved to ZF2 as likely to involve a BC break.
Posted by Evan Coury (evan.pro) on 2012-07-21T00:46:22.000+0000
Approaching RC1, we need to decide if anything is going to be done about this.
Posted by Ralph Schindler (ralph) on 2012-10-08T20:15:38.000+0000
This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2443