[dx] Posted September 18, 2012 Share Posted September 18, 2012 (edited) Hi, I have issue with this code. In my included php file there's var $something = 'x'; and in tpl file there's <?php echo $something; ?> But variable seems not to be defined. $page = (isset($_GET['page']) ? $_GET['page'] : 'site'); $option = (isset($_GET['option']) ? $_GET['option'] : 'main'); $allowed_pages = array('admin', 'site'); function func_controller($allowed_pages, $page = NULL, $option = NULL) { if (in_array($page, $allowed_pages)) { func_require('/controller/'.$page.'/'.$option.'.php'); }}function func_template($allowed_pages, $page = NULL, $option = NULL) { if (in_array($page, $allowed_pages)) { func_require('/view/default/'.$page.'/'.$option.'.tpl'); }} func_controller($allowed_pages, $page, $option);func_template($allowed_pages, $page, $option); var_dump(get_included_files());var_dump(get_defined_vars()); Best regards. Edited September 18, 2012 by Haris S Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2012 Share Posted September 18, 2012 The file is being included from inside a function, which means the code inside that included file is being executed in the scope of that function, which means that you need to define any global variables you want to use in the included code as global. It's the same issue as if the code was copied and pasted into that function instead of being included, the variables are not global inside the function. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 Also I've tried without function. Just func_require.. Link to comment Share on other sites More sharing options...
Ingolme Posted September 18, 2012 Share Posted September 18, 2012 You need to read up on scope in PHP. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 But what if I use only outside functions, it's same.. not defined variable func_require('/controller/'.$page.'/'.$option.'.php');func_require('/view/default/'.$page.'/'.$option.'.tpl'); Link to comment Share on other sites More sharing options...
Labtec Posted September 18, 2012 Share Posted September 18, 2012 I believe creating a global variable in one script makes it accessible to any script in your directory... $var = 1; function your_function(){ global $var; //now you can use this variable within your function.} Regards, Labtec. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 Yeah, but it's bunch of variables, I can't do global for every var.. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2012 Share Posted September 18, 2012 You can refer to them using the $GLOBALS array. If you're going to use global variables inside functions then you need to say they're global, there's not a way around that. You can use static classes to replace global variables, but if you insist on using global variables then you need to say that they are global. If you don't want to say that each variable is global then you don't get to use those variables. http://php.net/manual/en/reserved.variables.globals.php Link to comment Share on other sites More sharing options...
boen_robot Posted September 18, 2012 Share Posted September 18, 2012 But what if I use only outside functions, it's same.. not defined variable func_require('/controller/'.$page.'/'.$option.'.php');func_require('/view/default/'.$page.'/'.$option.'.tpl'); When we talk about scope, we're talking about the variables defined in'/controller/'.$page.'/'.$option.'.php' being available only within func_require(). And anyway, why do you need a function for that to begin with? What else is this function doing besides doing a "require" on the file? Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 Well, it's just including file with $_SERVER['DOCUMENT_ROOT'] Maybe it's unnecessary but whatever.. I have avoided functions func_controller and func_template so I only use func_require..and there's no need to do global $something; but still returns null Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2012 Share Posted September 18, 2012 You're going around in circles, we already told you that this is an issue with variable scope and we've given you several possible solutions. If you're still having the same problem then you haven't done one of the solutions. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 But what is point of this. // file1.php $something = 'value'; // file2.php include 'file1.php'; echo $something; // not defined Is that issue with my code, or what? What to do in this case.. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2012 Share Posted September 18, 2012 That case doesn't happen, if you set up those 2 files just like you wrote out you'll find that the variable is defined. Including a file is just like copying and pasting the code in place of the include statement, so it's the exact same as this: $something = 'value';echo $something; Obviously, that variable is going to be defined. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 In my case it's not, I've tried it. So like func_require(file_path);echo $something; I don't know why, it's basic php knowledge Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2012 Share Posted September 18, 2012 See, that second example in post 14 is different than the first example in post 12. The second example has a function call. That's why it doesn't work. The function call changes the variable scope. This is what we've told you through the entire thread. Like you said, it's basic PHP knowledge. Link to comment Share on other sites More sharing options...
boen_robot Posted September 18, 2012 Share Posted September 18, 2012 Replace func_require('/controller/'.$page.'/'.$option.'.php'); with require $_SERVER['DOCUMENT_ROOT'] . '/controller/'.$page.'/'.$option.'.php'; That is all. Link to comment Share on other sites More sharing options...
[dx] Posted September 18, 2012 Author Share Posted September 18, 2012 Argh.. Thanks :/ What a mess.. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now