...

View Full Version : Can a Class create a Variable ??



mOrloff
01-21-2010, 07:03 PM
I want to create a class which creates variables.
(If someone has already done this, please provide a link or something.)

The idea is to make a class that will instantiate variables and store a list of them so that they can all be unset in one fail swoop.

This is how I envision the usage:


$var = new Variables();

$var->new('testVar1','this is the content in this test variable');
$var->new('testVar2',NULL);

while(something blah blah){
$testVar2.='some content';
}

if($testVar2) echo $testVar1;

$var->unset(all);


But I just can't figure out how to get it to return a Var like that.
Let me know whether this needs more clarification.

(I'm kinda hoping that this hasn't already been done, because it sounds like a fun exercise :))

~ Mo

kbluhm
01-21-2010, 07:18 PM
This could be done using the $GLOBALS variable.


class Variables
{

protected $variables = array();

public function __set( $name, $value )
{
$this->set( $name, $value );
}

public function set( $name, $value )
{
$this->variables[$name] = $GLOBALS[$name] = $value;
return $this;
}

public function clear()
{
$GLOBALS = array_diff_key( $GLOBALS, $this->variables );
}

}



$var = new Variables;

$var->set( 'name', 'John' );

// or using __set():
// $var->name = 'John';

$name .= ' Smith';

echo $name; // John Smith

$var->clear();

isset( $name ); // FALSE

Dormilich
01-21-2010, 07:30 PM
when using global variables (and I donít mean the $GLOBALS array), always be aware of Code Injection. maybe you want to look at the (Abstract) Registry Pattern.

mOrloff
01-29-2010, 04:22 PM
So, getting back to this, what is the difference between using

...


...
public function __set( $name, $value )
{
$this->set( $name, $value );
}
...
...
$var->set( 'name', 'John' );
...


And this




...
public function set( $name, $value )
{
$this->variables[$name] = $GLOBALS[$name] = $value;
return $this;
}
...
...
// or using __set():
// $var->name = 'John';
...



I'd like to learn why it works both ways so I can learn about those methodologies and become a more well rounded "developer" (I put that in quotes because that may be a bit of a stretch ;)).

~ Mo

kbluhm
01-29-2010, 04:59 PM
__set() is a magic method for setting properties not already defined.

http://php.net/manual/en/language.oop5.magic.php

Dormilich
01-29-2010, 05:00 PM
the first one is a method call, the second a variable assignment.

Dormilich
01-29-2010, 05:00 PM
__set() is a magic method for setting properties not already defined.

it also works on non-public properties.

kbluhm
01-29-2010, 05:06 PM
it also works on non-public properties.

Well ok, properties not publicly accessible.

mOrloff
01-29-2010, 05:11 PM
So much good information, so little time :D

No, but honestly, thanks-a-bunch.
If there's anything else, keep it coming.

~ mo

Dormilich
01-29-2010, 05:14 PM
you can use __set() & __get() to mimic read-only properties.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum