1

I have 2 files, class.inc.php and index.php.

class.inc.php contains Myclass and few functions, index.php file spits out functions from class.inc.php.

Now I need to create a function which will include/require a file and that function should actually require that file in index.php not in class.inc.php.

Yes I know I can place my file in a function and call it that way but we have to keep it in files because of some future MVC overrides. I do not want to include a file in index.php directly either if all possible. So is there a way to do this?

In my index.php I should be able to do this:

Myclass::include(PARAMS);

and that should include a file name params.php located somewhere else.

I tried this in class.inc.php

abstract class Myclass {
    static function load($filename){
        require_once $filename;
    }
}

and this in

index.php

Myclass::include(PARAMS);

but none of my variables from params.php are visible in index.php because they seem to be in class.inc.php.

9
  • Why does it make a difference in which file the include is done? Commented Mar 29, 2013 at 0:33
  • @Jack because we need to include several layout files , 15 of them , which will later be able to be overwritten via MVC , so we are trying to stay away from requires and includes in index and call everything from class functions if possible. I edited the post with more info Commented Mar 29, 2013 at 0:36
  • When including a file in a function, variables defined in the file will be visible only in the scope of the function. Commented Mar 29, 2013 at 0:38
  • What's inside params.php? Commented Mar 29, 2013 at 0:38
  • yes , we got that so far , thus I am asking if there is a different way maybe? Commented Mar 29, 2013 at 0:39

1 Answer 1

1

include, require and friends basically act as though you had copied-and-pasted the contents of the file to that location. So if you run them in a function, any "bare" variables, which would be global if you ran the file on its own, are instead local to that function.

Generally speaking, the answer in modern PHP code is simply not to use any global variables - a file should contain only functions, or better still, only namespaces and classes, with all the variables wrapped up in those.

If you really need to have global variables, however, you can use the global keyword either in your include function or at the top of the included file.

So for instance if your included file defines a variable $config which needs to be accessed elsewhere, at the top of the file you can write global $config; to push it out of any scope you're in when you include/require it.

Sign up to request clarification or add additional context in comments.

4 Comments

trying to stay away from globals, and seems that only way to do what we need is call all params inside a function first
was hoping for bit of flexibility just because of the MVC's that we have in front of us
The only compromise I can suggest is having one or two global arrays (hence my example of $config) so you only need to global that one variable, but other code can still see $config['foo'] etc. Although if you can do that, you can probably do Config::set('foo', 'bar')/Config::get('foo') instead, and make your code more structured...
Also, I'm confused by the phrase "trying to stay away from globals", since this question is all about making global variables work - if you don't need global variables, then your approach will work fine, as functions and classes aren't scoped that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.