I'm trying to make a wrapper class for the primitive types in php but I encountered a problem while making a 'string' class;
If a create a new class like $str = new _string(); it would be nice if i could still assign the value with $str = 'foo'; but of course that will overwrite $str and the class will be destroyed.
So I had the (possibly crazy) idea of accessing the class with $str and its value with $$str by creating $$str in the global scope when instantiating $str
The problem is global $$this->name doesn't work as I'd expect, infact I'm not quite sure whats going on.
class _string {
private $name;
public function __construct($name) {
$this->name = 'str_' . $name;
global $$this->name; // <---this doesn't work properly
$strName = 'str_' . $name; //assigning a global variable variable
global $$strName; //from a local variable works fine
}
public function __toString() {
return $this->name;
}
public function rev() {
$strName = $this->name;
global $$strName;
$$strName=strrev($$strName);
$$this->name=strrev($$this->name);
} // ^
// |
} // |
// |
$str = new _string('str'); // |
$$str = 'hello'; // |
$str->rev(); //Warning: Attempt to assign property of non-object
echo $$str; //olleh
My aim is to avoid using $str->value='foo' and get as close to $str='foo' as possible
Is this even possible? Or does anyone have a better suggestion?
$$this->name--${$this->name}$str = new _string(),$str ='foo';,$str->rev();,echo $str // oofas possible