PHP Variable Scope Issue?

The file is being included within the context of the load_snippet() function, and therefore has only those variables which exist within that function. One way to modify this is to make your function accept two variables: the filename and an array of values public function load_snippet($snippet, $content) { if (is_array($content)) extract($content); if (file_exists("app/". $this->APP_TYPE."/snippets/".

$snippet. ". Php")) { include "app/".

$this->APP_TYPE. "/snippets/". $snippet.

". Php"; } else { include 'common/txt/404. Txt'; } } Then $arr = array('variable' => ' '); load_snippet('slider', $arr).

The file is being included within the context of the load_snippet() function, and therefore has only those variables which exist within that function. One way to modify this is to make your function accept two variables: the filename and an array of values. Public function load_snippet($snippet, $content) { if (is_array($content)) extract($content); if (file_exists("app/".

$this->APP_TYPE. "/snippets/". $snippet.".

Php")) { include "app/". $this->APP_TYPE."/snippets/". $snippet.

". Php"; } else { include 'common/txt/404. Txt'; } } Then $arr = array('variable' => ' '); load_snippet('slider', $arr).

– Archio Jul 4 at 16:32 If you want the load_snippet() function to be generic, and not have a whole bunch of global stuff defined in it, this is, I think, the only way. Don't define your variables and then put them in an array.In my code, generally in this case the array is a database row, $row = Db_Load_Array($sql); Render::snippet('snippet_name', $row); does the job quite neatly. – TRiG Jul 4 at 16:40 Thanks, this solution works the best.

I defined my variables straight into the $arr array so it wasn't redundant defining them, and now I can access all of them in the snippets I use. – Archio Jul 4 at 16:52.

I think include inside a function makes no sense to me... I think that you should put in function global $variable; Note that include will put the code inside the function(include will be replaced by code) as I know..

You can include whenever you want (it does not make sense from a point of view of a clean code), and the include will be replaced by the content of the file durring compilation. I've seen such approach in Joomla modules (some early version of Joomla), and there was a scope issue because the module was using global, but the variable was not in the global scope, it was in the function's scope (in the scope of the function that includes the file, and the file was declaring class/methods that were referencing global $variable) – Darhazer Jul 4 at 16:23 I know, includeing inside a function seems awkward to me, too... but the thing is, there must be a better DRY & MVC method of doing this, instead of constantly going include("app/game/snippets/snippet. Php").

– Archio Jul 4 at 16:25 In terms of putting global $variable in the function, when I have many different variables I need to reference in different snippets, wouldn't that be redundant? – Archio Jul 4 at 16:30 You could create a function to output your stuff inside snippet. Php .

And call that instead of including.. however it might require extra code. – Parhs Jul 4 at 16:30 @Chromium what do you mean with redurant? Well its not the best way to keep a variable – Parhs Jul 4 at 16:32.

The way you are doing it is an ugly one, but you can use global $variable inside the snipped to refer to the variable. However if you include the snipped inside a function or a method, you'll have to make the variables in that function/method global as well.

You are including inside a class. Which means that the included file has the same variable scope as the line of code which includes it has. TO fix this all you need to do is put global $variable; Above the include.

If you need $variable inside of the App::load_snippet() method, it would probably be best to pass it in: public function load_snippet($snippet, $var=' world'){ if(file_exists("app/". $this->APP_TYPE. "/snippets/".

$snippet. ". Php")){ include "app/".

$this->APP_TYPE. "/snippets/". $snippet.".

Php"; }else{ include 'common/txt/404. Txt'; } } //do something with $var } You can set a default for when $variable hasn't been set.No globals, no out of scope variables.

That would most definitely work. However, I'm going to be loading many different snippets with many other variables, and my code would be unmanageable if I had to keep adding variables separately to a function with 50+ parameters. Is there a way to do this that is more DRY?

– Archio Jul 4 at 16:34 @Chromium, I'm a bit confused as to why you'd need 50+ parametres? If each call to snippet needs different information in $var, then pass different information. If you need to pass a set of variables, then define $var as an array and use named indices.

Or, if all your information is related, define $var as an object of, say, Class SnippetAuxData. In any of these cases, you only pass one $var. However you do it, if $var or your $variable will be of substantially different forms, then your load_snippet function will need conditional logic to deal with those forms.

– dnagirl Jul 4 at 17:31.

Instead you can use the constants like define('VARIALABLE','value'). Which will be available to you anywhere in your file.

In my code, I use a public load_snippet function of a class when I need to include HTML or PHP snippets. (I do this instead of a direct include_once because the directory structure varies depending on certain variables). The snippet renders perfectly, except that PHP gives an undefined variable error if I try to reference $variable in the slider code.

Any help you can give me is much appreciated.

That's pretty oldschool and not recommended. If you want to use variables whereever you want consider using sessions of passing through variables if you're on an object oriented tour ;).

Alright, thats more or less what I thought. Thanks for the advice. – Dylan Nov 23 '09 at 8:32 You're Welcome :) – Ben Fransen Nov 23 '09 at 8:33.

There is a $GLOBALS variable and a globals keyword. Check out example 1 and 2 on this page in the documentation. But using a global variable is usually a sign of bad structure in your code.

Yah, I was looking at that page earlier. I'll try to come up with a different way to do it. Thanks.

– Dylan Nov 23 '09 at 8:33.

I don't think variables can be globally visible the way you want bnut functions are. You can declare two functions to access you data : on for reading, one for writing. But anyway beware : if you change it from many places you will have hard times understanding what's going on when faced with a bug!

Sometimes a variable available in global scope is not accessible via the 'global' keyword or the $GLOBALS superglobal array. I have not been able to replicate it in original code, but it occurs when a script is run under PHPUnit. PHPUnit provides a variable "$filename" that reflects the name of the file loaded on its command line.

This is available in global scope, but not in object scope. For example, the following phpUnit script (call it GlobalScope. Print "Method scope GLOBALSFILENAME ".

If you run this script via "phpunit GlobalScope. Global scope FILENAME /home/ktyler/GlobalScope. PHPUnit 3.4.5 by Sebastian Bergmann.

Print "Method scope GLOBALSFILENAME ". By doing this, both "global" and $GLOBALS work! I don't know what it is that PHPUnit does (I know it uses Reflection) that causes a globally available variable to be implicitly unavailable via "global" or $GLOBALS.

But there it is.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions