Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^160) CHAPTER 10 ■ SPL ITERATORS
To follow the examples in this section, you will need to create an .ini file like the one
shown in Listing 10-18.
Listing 10-18. A Sample ini File (test.ini)
[group1]
mykey=myvalue
DbaReader
DbaReader is the foundation iterator for the DBA iterators. Its constructor takes two parameters:
a file and a handler. In the case of an .ini file, the handler parameter is inifile.
You can use the DbaReader iterator to read the sample .ini file in Listing 10-18 using the
code shown in Listing 10-19.
Listing 10-19. Using a DbaReader Iterator
require_once('path/to/php-src/ext/spl/examples/dbareader.inc');
$it = new DbaReader('test.ini', 'inifile');
print_r(iterator_to_array($it,true));
Array
(
[[group1]] =>
[[group1]mykey] => myvalue
)
IniGroups
If you just want to determine the groups in an .ini file, you use the IniGroups iterator. This iter-
ator is a type of KeyFilter iterator that will provide a list of .ini file groups. A demonstration is
shown in Listing 10-20.
Listing 10-20. Using an IniGroups Iterator
require_once('path/to/php-src/ext/spl/examples/inigroups.inc');
$it = new IniGroups('test.ini', 'inifile');
print_r(iterator_to_array($it,true));
Array
(
[group1] => group1
)
McArthur_819-9C10.fm Page 160 Friday, February 22, 2008 9:08 AM

Free download pdf