New Idea! Class Variable Scope all to Private + one Get Set
Posted: Mon Sep 14, 2009 3:19 pm
Ok here's the thing, I'm just sharing an idea here to know what you people think of it?
Suppose I have a class:
If I want to retrieve the static variable fro the public scope I can do the following:
which outputs:
but if the class static variable was declared private like this:
so that I can restrict its manipulation from either a class extending this one or from the public scope.
But maybe I would like to be able to get the value of this static variable, or set it, or maybe be able to still do both...
Then only the second situation (with a public static declared method) would allow me to do so...
Suppose I have a 100 or more variables on which I would like to be able to do so... that means I would have to declare get and set methods for each of them (200 methods or so!!!).
I surely don't want to program 200 methods!
So I was trying to find a way to only have one get method and one set method in each of my classes...
This is what I've come up with
an interface containing some constants:
and a class:
called this way:
outputs:
but this would throw me an error:
Note: I use prefixes in front of the variables name to distinguish:
Suppose I have a class:
Code: Select all
class firstClass{
public static $variable = 'whatever';
public static function get_variable(){
return self::$variable;
}
}
Code: Select all
echo firstClass::$variable;
echo firstClass::get_variable();
Code: Select all
whatever
whatever
Code: Select all
private static $variable = 'whatever';
But maybe I would like to be able to get the value of this static variable, or set it, or maybe be able to still do both...
Then only the second situation (with a public static declared method) would allow me to do so...
Suppose I have a 100 or more variables on which I would like to be able to do so... that means I would have to declare get and set methods for each of them (200 methods or so!!!).
I surely don't want to program 200 methods!
So I was trying to find a way to only have one get method and one set method in each of my classes...
This is what I've come up with
an interface containing some constants:
Code: Select all
interface constants_int{
/**@CONST */
// var flags
const NO_AUTH = 0;
const PUB_FLAG = 1;
const PROT_FLAG = 2;
const PRIV_FLAG = 3;
// class variable prefixes
const INI_PREF = '_I';
const DEF_PREF = '_D';
const FLAG_PREF = '_F';
const STAT_PREF = '_';
}
Code: Select all
class secondClass implements constants_int{
private static $_D_variable = 'whatever';
private static $_F_variable = array('GET'=>self::PUB_FLAG,'SET'=>self::PRIV_FLAG);
private static $_variable = null;
/*********************************************
* Method that initialise the static variables
* still not set to their default values and
* performs some additionnal prerequired tasks
* and verifications for this class use
*/
public static function ini(){
$classVars = get_class_vars(__CLASS__);
foreach($classVars as $varName=>$varValue){
if($varValue===null){
if
( preg_match('/\A_[^DFI]/U',$varName) &&
array_key_exists(self::DEF_PREF.$varName,$classVars)
)
{
self::$$varName = self::${self::DEF_PREF.$varName};
}
}
}
}
/*********************************************
* Method to get different get flagged static and object
* variables from either an array (which returns an
* associative array) or from a string (which returns
* the direct value).
*/
private static function _get($getFlag,$mixed){
$return;
if(is_array($mixed)){
$return = array();
foreach($mixed as $toGet){
$return[$toGet] = self::get($getFlag,$toGet);
}
}
elseif(is_string($mixed)){
if
( array_key_exists(self::STAT_PREF.$mixed,get_class_vars(__CLASS__)) &&
isset(self::${self::FLAG_PREF.self::STAT_PREF.$mixed}) &&
self::${self::FLAG_PREF.self::STAT_PREF.$mixed}['GET']<=$getFlag
)
{
$return = self::${self::STAT_PREF.$mixed};
}
else{
trigger_error('',E_USER_WARNING);
}
}
else{
trigger_error('',E_USER_WARNING);
}
return $return;
}
/*********************************************
* Method which gets public flagged variables
* from a public dependancy context or higher
*/
public static function get1($mixed){
return self::_get(self::PUB_FLAG,$mixed);
}
/*********************************************
* Method which gets protected flagged variables
* from a protected dependancy context or higher
*/
protected static function get2($mixed){
return self::_get(self::PROT_FLAG,$mixed);
}
/*********************************************
* Method which gets private flagged variables
* from a private dependancy context or higher
*/
private static function get3($mixed){
return self::_get(self::PRIV_FLAG,$mixed);
}
/*********************************************
* Method to set different set flagged static and object
* variables from either an associative array
* or from two arguuments (variable name followed by
* its value).
*/
private static function _set($setFlag,$mixed,$value=null){
if(is_array($mixed)){
foreach($mixed as $toSet=>$value){
self::_set($setFlag,$toSet,$value);
}
}
elseif(is_string($mixed)){
if
( array_key_exists(self::STAT_PREF.$mixed,get_class_vars(__CLASS__)) &&
isset(self::${self::FLAG_PREF.self::STAT_PREF.$mixed}) &&
self::${self::FLAG_PREF.self::STAT_PREF.$mixed}['SET']<=$setFlag
)
{
self::${self::STAT_PREF.$mixed} = $value;
}
else{
trigger_error('',E_USER_WARNING);
}
}
else{
trigger_error('',E_USER_WARNING);
}
}
/*********************************************
* Method which sets public flagged variables
* from a public dependancy context or higher
*/
public static function set1($mixed,$value=null){
return self::_set(self::PUB_FLAG,$mixed,$value);
}
/*********************************************
* Method which sets protected flagged variables
* from a protected dependancy context or higher
*/
protected static function set2($mixed,$value=null){
return self::_set(self::PROT_FLAG,$mixed,$value);
}
/*********************************************
* Method which sets private flagged variables
* from a private dependancy context or higher
*/
private static function set3($mixed,$value=null){
return self::_set(self::PRIV_FLAG,$mixed,$value);
}
}
secondClass::ini();
Code: Select all
echo secondClass::get('variable');
Code: Select all
whatever
Code: Select all
echo secondClass::set('variable','foo');
- '_D': default value initialise when calling secondClass::ini()
- '_F': array containing the Get and Set flags
- '_': static variables