How can I replace GLOBALS in YII

Hi,

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?

Thanks.

The problem is : GLOBALS can’t work under an other class :), I will find another solution to implement this example…

I found the solution, it can be useful for many people:

The class:




class assign

{

    public $varname;

    public $value;    

    

    public function controller ()

    {

	

	  $this->varname = $this->value;

	  

    }

}



In the controller:




$str = 'toto';

$assign= new assign();

$assign->varname = &$str;

$assign->value = 'titi';

$assign->controller();//It works !!




Can I please ask what you are trying to do here.

I understand that you might be wrapping legacy code and have to do this, but would like to understand why you are doing what you are doing.

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.

Regards

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.

Every class in Yii is a "simple" class. It is still php. Very good php but still php…

It still PHP, and we must find the solution if we would like to be good software designers :)

The ad hominem was unnecessary. I mentioned domain objects, which is what you are referring to as "simple php classes".

Personally, I think you’ll discover a few problems with your “solution”. Good luck.

Excuse me sir, but I think that you are wrong.

‘static’ should be useful for you




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();



output is val1…val1

also


Yii::app()->session[];

is cool ;)

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.

Thanks! it is another way to do it…