I am building a class that turns a flatfile into a virtual database, I am trying to take the values of an array as the column names instead of indexes when retrieving data like so…
$db = new Database($config, array('first', 'second', 'third', 'fourth'), true);
The array is sent to the constructor in database.php:
public function __construct(Config $config, array $constants = null, $caseInsensitive = false) {
$this->_config = $config;
if(!is_null($constants))
$this->defineColumns($constants, $caseInsensitive);
return true;
}
Which is passed on to defineColumns():
private function defineColumns($constants, $caseInsensitive) {
for ($i=0;$i<count($constants);$i++)
define($constants[$i], $i, $caseInsensitive);
}
This works and I can now use first to access column 0, second to access column 1 and so on…
However the define() function seems to make the constants global and accessible from outside the class instance.
I want each set of declared constants to be scoped to the instance alone allowing me to use the same constant again in another instance of the class to access a different column.
Does anyone know a way of doing this?
I’m afraid is not possible if you want to use constants. Constants are global.
By not using constants, you can store that names in a class static member and use them with something like
$myinstance->get('third').