Recommend this page to a friend! |
Classes of Christian Vigh | Variable Store | README.md | Download |
|
DownloadINTRODUCTIONA VariableStore is simply some kind of Dictionary object that is able to store variable name/value pairs, allowing variable values to reference the contents of other variables in the same store. Defining a VariableStore and adding variables to it is really simple :
Variable values can reference other variables ; when retrieving their value, their contents will be expanded :
You have a faster way to initialize a VariableStore object, since you can pass an array of variable name/value pairs to the constructor :
You can even pass a VariableStore object itself to the constructor :
In fact you can pass any number of arrays or VariableStore objects to the constructor. There is also a Load() method that accepts the same kind of arguments, but that you can call later, after object instantiation :
Last, but not least, you can include your environment variables in a variable store ; this can simply be done by providing the appropriate option flags to the constructor :
That way, all the environment variables defined in your shell session will be included into your variable store object. And, even more, every change you will do to one of your environment variables will be also reflected in your session environment. To be sure of what is inside your VariableStore object, simply call the ToArray() method :
Variable stores accept various options when instantiating them ; you can also check for variable existence, undefine variables, retrieve variable values (either expanded or not). Have a look at the Reference section for more information. WHY USING A VARIABLE STORE ?The first obvious advantage of using a variable store is expanding variable values that reference other variables defined in the same store, and not elsewhere in your code or in your session environment. A more realistic usage is provided by the following class, which allows to parse and save .INI files : http://www.phpclasses.org/package/9413-PHP-Load-and-edit-configuration-INI-format-files.html Coupled with the VariableStore class, it will allow you to reference variables from within your .INI file, and even provide a whole section that will allow you to define variables later referenced in setting values defined in the same file. DEPENDENCIESThis packages uses the following one : http://www.phpclasses.org/package/9904-PHP-Store-associative-array-with-case-insensitive-keys.html The source code for this class has been provided here for your convenience, but it may not be the latest version. REFERENCEThis section provides a reference to the VariableStore object. Referencing other variables from a variable's valueWhen setting a variable's value, the following constructs are considered as variable references :
Circular variable references will generate an exception, as in the following example :
Note that variable values are not expanded when they are defined, but only when they are retrieved using the array access operator. Only the variables defined in you VariableStore instance can be referenced in variable values. METHODSCONSTRUCTOR
Builds a VariableStore object, using the specified parameters, which can be in any order :
NOTES :
Define
Defines a new variable in the variable store. The $value parameter can contain reference to other variables defined in the variable store, but they will only be expanded when their value will be retrieved using the array access operator. If the $export parameter is set to true, any modification to the variable (value change or deletion of the variable using the Undefine() method) will affect the corresponding variable in your environment. Expand
Expands the specified string value, using the variables defined in the current VariableStore object. If the $accept\_escapes parameter is set to true, a backslah before a character in the variable's value will return the character as is. Export
Exports the specified variable to the environment (ie, the variable value will be able to be retrieved using the getenv() function). IsDefined
Checks if the specified variable is defined in the store. Load
Adds any number of arrays of variable name/value pairs or VariableStore objects to the current variable store. Arrays can contain nested arrays or VariableStore objects. ToArray
Converts a VariableStore to an associative array containing variable name/value pairs. If the $expand parameter is set to true, variable contents will be expanded before return. Undefine
Removes the specified variable from the variable store. PROPERTIESOptionsCombination of OPTION\_\* flags specified to the constructor. CONSTANTSOPTION\_\* constantsA combination of the following flags, which influence the behavior of the VariableStore object :
INTERFACESThe following section lists the interfaces implemented by the VariableStore class. ArrayAccessAllows access to variable values using the following notation :
Note that this will expand the contents of the myvar variable ; to retrieve the raw contents, prepend a sharp sign to the variable name :
CountableAllows for the use of the count() function on a VariableStore object ; returns the number of variables currently defined. IteratorAggregateAllows for the use of the foreach() construct on a VariableStore object. |