I would like to change a value of a variable in my controller using a class based in GLOBALS var:
The class:
class assign
{
public $varname;
public $value;
public function controller ()
{
settype($this->varname,'string');
$GLOBALS[$this->varname] = $this->value;
}
}
In the controller:
$str = 'toto';
$assign= new assign();
$assign->varname = 'str';
$assign->value = 'titi';
$assign->controller(); //$str will be 'titi'
I see that GLOBALS can’t work under Yii Framework, how can I resolve this problem?
This code shows how to access to variables defined in the controller class from another class (
example of assigning a value to a variable) without using the GLOBALS var. In the first time I found problem with GLOBALS, so I tried to use variables references to do what I am doing.
Are you saying that you want to pass data between two controllers? That seems odd.
You keep saying "another class". What is that class? A model, a view, or a controller.
If it’s you own domain object or a component, then just set the appropriate member in the usual way. If you need that domain object in many places, then just pass it around.
I’m sorry, but I still don’t understand the problem that you are trying to solve.
Of course, you might have a valid case for a singleton, but without understanding the problem it’s impossible to say.
I see that you really do not understand the problem, if you concentrate, you’ll see there are two general classes (under any environment [yii, simple php classes…]) , and the problem is already solved, I just try to access to declared variables into Yii controller from a simple PHP class (not component, not another controller), just a simple class in my own library.
class vars
{
public static $data;
}
vars::$data['str1']='val1';
echo vars::$data['str1'];
class someClass
{
public function someMethod()
{
echo vars::$data['str1'];
}
}
echo '..';
$a=new someClass();
$a->someMethod();
As you say phpdevmd static members is one idea, static methods perhaps, dare I mention singletons or Yii’s own Yii::app()->params. Saving state in session is handy, but only in an OO way, imo – and we kinda have user() for that in Yii. Hey, why not web services, since they’re so easy in Yii <joke>!
That’s why I think the OP needs to look a little wider before jumping in.