'global' keyword not working in views?

I have to port old (only partly object oriented) code to Yii.

There are some libs that uses a lot of global vars in functions.

I want to include these libs inside a view.

Now I have detected that the keyword ‘global’ doesn’t work in a view or component files (controller …)

It’s easy to reproduce - put this code into a view file.

$a = 1;

function test(){

  global $a;




will display: a=

not the expected a=1

Any explanation/solutions for this strange behavior?

When you declare $a on top of the view file, you’re not in the global scope. So just try:

global $a=1;

Thanks, that helps :slight_smile:

But seems to be a hard work to migrate:

checking hundreds of variables if they are known in a function or not :frowning:

If I had more time I would rewrite the project in ‘real Yii’…

You should include this libs in your index.php instead. Then you are in global scope and all functions should work just like in your legacy app.

Yes I did this on testing, but there are to much libraries.

I don’t want to load all on every request independent of the action I want to execute.

The old code is based on the modular concept of the Fusebox where the libs are loaded selective.

Complete rewriting the code will be the best…