API IniFile __construct - SierraKomodo/INILib GitHub Wiki
IniFile::__construct
- Constructs a new IniFile
object
Description
public IniFile::__construct(string $fileName [, bool $readOnly = false [, int $scannerMode = INI_SCANNER_RAW ]])
Construct a new IniFile
object
Parameters
fileName
The file to read. This is passed directly to SplFileObject::__construct()
as the filename
parameter to create an internal file handler.
NOTE: The file must already exist. If IniFile
is being used to create a new/default INI file, it's recommended to use mkdir()
and touch()
to create the filepath first.
readOnly
Flag to specify whether the INI file should be treated as 'read-only' or allow write/modification operations. If set to true, the created SplFileObject
will use a read-only open_mode
, and any IniFile
methods that would modify the INI data stored in memory or the INI file itself will throw a IniFileException
exception with code IniFileException::ERR_READ_ONLY
.
scannerMode
The PHP built-in INI scanner mode to use. See the scanner_mode
section of PHP.net's parse_ini_string
documentation for specific details. Current parameter validations will require one of INI_SCANNER_NORMAL
, INI_SCANNER_TYPED
, or INI_SCANNER_RAW
.
NOTE: It is highly recommneded to use the default value INI_SCANNER_RAW
if you intend to write data to the INI file, and perform comparisons/modifications using string comparisons. This is because the scanner mode only affects the reading of the INI file, and will not change values when they are being written to a file. As an example: Assume you have an INI file with key bSomeBooleanValue=off
. INI_SCANNER_NORMAL
will parse this as an empty string ''
, INI_SCANNER_RAW
as string 'off'
, and INI_SCANNER_TYPED
as boolean false
. Upon writing this data back to file, without any changes, this key will now have an empty value, a value of off
, or a value of 0
, respectively. A possible correction to this behavior is planned for future updates.
Return Values
No value is returned (void)
Errors/Exceptions
- Throws a
IniFileException
with codeIniFileException::ERR_FILE_NOT_EXIST
if$parFile
cannot be opened - Throws a
IniFileException
with codeIniFileException::ERR_INVALID_PARAMETER
if$scannerMode
is not one of the accepted scanner modes - Throws a
IniFileException
with codeIniFileException::ERR_FILE_NOT_READABLE
if$parFile
is unreadable according toSplFileObject::isReadable()
Changelog
Version | Description |
---|---|
v1.0.0 | IniFile::__construct() introduced |
Examples
Example #1
Assuming config.ini
has the following contents:
[Section]
key1=apple
key2=banana
key3=pear
This example opens an INI file and retrieves the generated data array
$iniFile = new IniFIle('config.ini');
$dataArray = $iniFile->fetchDataArray();
var_dump($dataArray);
The above example will output something similar to:
array(1) {
["Section"]=>
array(3) {
["key1"]=> string(5) "apple"
["key2"]=> string(6) "banana"
["key3"]=> string(4) "pear"
}
}