In PHP, what is the right way forward for the following situation:
I have a 'central' vars.php which contains some parameters like MySQL hostname, user, pass, a $Test variable which indicates if the code is running in a test environment or not, and some other variables which I use throughout my site.
Then I have a functions.php which contains all the functions I could use throughout my site.
In some of these functions, I might need some the variables out of the vars.php file.
What is the right way to make the vars.php available to 'everywhere' in PHP?
Now I am doing it like this:
vars.php:
<?php
if(strstr($_SERVER['HTTP_HOST'],"localhost"))
{
$Test = true;
}
?>
functions.php:
<?php
function DoSomething()
{
include "vars.php";
if($Test)
{
$String = "Test is true!";
}
else
{
$String = "Test is false!";
}
return $String;
}
?>
index.php:
<?php
include "vars.php";
include "functions.php;
$DoSomething = DoSomething();
echo $DoSomething;
?>
While this does work, I have to include the vars.php in each function I define, this doesn't seem like a 'nice' solution to me.
Isn't there a way to define the variables in vars.php directly in the global scope, and be able to use them inside functions directly without having to worry about including them?
Thanks!
The right way would be using constants instead of variables.
You're looking for global variables. If you include the file at the top of your page and then declare each one as a global within your function like this:
include('vars.php');
function whatHost()
{
global $host;
echo $host;
}
it will work. This is very bad practice though, as global state is very bad 95% of the time. PHP has powerful object-oriented features which allow you to encapsulate functionality and state within objects. I suggest you read up on that and learn about more proper ways of storing application configuration. Note that for this particular situation, you could just use constants (:
Use constants. Define all together and always on top of classes/files.
Constants are uniques and we use them as config info or static/const data.
If u are sue DEFINE remember use const as a string not a var, example:
define("BASE_PATH", "C://base/");
$dir = BASE_PATH . "images/dropbox.txt";
http://php.net/manual/en/language.constants.php
Related
I have a require for a file in my php code. This require exists twice in the code.
require 'test.php';
echo $result;
require 'test.php';
Is there a way to clear all the variables the first require sets before running the second require on the exact same file?
You could wrap the require in a function, if you don't use global vars. In that case all the variables you create will be limited to the function scope.
function includeFile() {
require 'test.php';
echo $result;
}
includeFile();
includeFile();
But it would be much better to create a class as it was suggested in the comments.
IF you declare all your variables at the beginning of your require like:
$var = false;
$var2 = 0;
$var3 = '';
// etc...
You wouldn't have to do anything other than require the file again - the variables would automatically be reset to an initial state by these declarations.
That being said, what you are looking to do cries out for a different approach. Either encapsulate that functionality into a function or a class depending on how you need to use it. Basically, if you have some code you are repeating throughout your codebase, you should modularize it, and definitely not pollute the global scope with variables only needed for that intermal functionality.
There are two things you can do. unset() will delete all those variables. That answers your question.
What you should do is look into OOP and classes. Make a class, then make an instance of that class. You can then make a new class and have completely different values and data, ultimately disabling the need to unset() and require again.
I did not test this code!
Point is get the list of defined vars before inclusion, include file, get the list of defined vars after inclusion, compute difference, unset global variables based on that difference, include file.
See manual entries on get_defined_vars and unset.
<?php
$old_vars = get_defined_vars();
require 'test.php';
$new_vars = get_defined_vars();
echo $result;
$diff_vars = array_diff_key($old_vars, $new_vars);
while ($var = key($diff_vars)) {
unset($GLOBALS[$var]);
next($diff_vars);
}
unset($old_vars, $new_vars, $diff_vars);
require 'test.php';
Makes sense?
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).
I had some issues with variable scopes, so I've narrowed down the problem to this: let's say I define a variable within my page:
$variable = 'Hello World!";
Then, I need load in a snippet:
$APP->load_snippet("slider");
The snippet renders perfectly, except that PHP gives an undefined variable error if I try to reference $variable in the slider code. If I directly include the php file, it works as expected, so I don't understand why I'm having this problem, since this is the load_snippet function:
public function load_snippet($snippet){
if(file_exists("app/".$this->APP_TYPE."/snippets/".$snippet.".php")){
include "app/".$this->APP_TYPE."/snippets/".$snippet.".php";
}
else{
include 'common/txt/404.txt';
}
}
Any help you can give me is much appreciated.
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' => 'Hello world!');
load_snippet('slider', $arr);
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..
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
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='Hello 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.
Instead you can use the constants like define('VARIALABLE','value'). which will be available to you anywhere in your file
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.
I'm a php newbie (but long time developer in other languages) and I'm trying some example db connections in "PHP, MySQL, & JavaScript". It shows an example file to include db connection variables (servername, username, password, database, etc.). I have a php file which has a handful of functions I wrote and one of them has a few SQL queries. For whatever reason, calling require_once in that file doesn't output any errors (I have E_ALL config'd) yet those variables in my database php file are null.
I called an echo with all the variables within that function to see what the heck is going on and of course it prints a blank line. What in the world is out of scope? I have to be missing something simple.
Here's an example of what I'm doing
db_login.php
<?php
$db_server = 'localhost';
// ....
?>
functions.php
<?php
require_once('db_login.php');
function myfunction() {
echo "$db_server";
// ...
}
?>
Call me crazy, but shouldn't this be simple enough to work?
PHP doesn't have function scope like Javascript, so you do not have access to the variables in db_login.php inside the functions of functions.php.
There are multiple ways of dealing with this. Due to your probable use of the server name global constants would probably be a good solution, since nothing can change them.
In your case you can do:
<?php
require_once('db_login.php');
// You have access to $db_server here.
// Create a constant.
define("DB_SERVER", $db_server);
function myfunction() {
// Using a constant. Note that there is no "$".
echo DB_SERVER ;
// Constants are interpreted inside double quotes too
echo "\nMy constant is DB_SERVER";
// ...
}
?>
In your case having the server name as a constant is probably appropriate. If you are dealing with something that you want to treat as a true variable, you can pass the variable into the function by value or by reference:
myfunction($variable);
// by value
function myfunction($pass_variable_to_me_by_value)
{
echo $pass_variable_to_me_by_value;
// ...
}
function myfunction(& $pass_variable_to_me_by_reference)
{
echo $pass_variable_to_me_by_reference;
// ...
}
As a note, in your case, using the global keyword or the $GLOBALS array inside a function is essentially the same as passing by reference., but if you are not passing from the global scope they can be very different (in a Class or from another function for example).
The variables you declare in db_login.php are globals. In order to access them in your function, you need to use the $GLOBALS variable, e.g. $GLOBALS['db_server'], or declare them as global inside your function using the global keyword, e.g. global $db_server.
Inside of the function "myfunction" you don't have access to these variable...
See more in: http://php.net/manual/en/language.variables.scope.php
I use settings.php to store general settings for my application. When I load this settings file, I can use the variables defined in settings.php in the script itself, but not within any functions I define in it.
For example, in my class definition, myclass.php:
<?php
$preIP = dirname(__FILE__);
require_once( "preIP/settings.php" );
class MyClass {
...
public function foo() {
echo $variable_from_settings;
}
}
The code in the function foo() will not work (the variable will not be defined).
The settings.php file looks like this:
$variable_from_settings = "bar";
Thanks,
How about putting
global $variable_from_settings
before
echo $variable_from_settings;
You could do the following if you don't want to put global $variable_from_settings; everywhere.
echo $GLOBALS['variable_from_settings'];
However it's probably better to use a singleton to contain your settings as suggested in create superglobal variables in php?
I have a function that includes a file based on the string that gets passed to it i.e. the action variable from the query string. I use this for filtering purposes etc so people can't include files they shouldn't be able to and if the file doesn't exist a default file is loaded instead.
The problem is that when the function runs and includes the file scope, is lost because the include ran inside a function. This becomes a problem because I use a global configuration file, then I use specific configuration files for each module on the site.
The way I'm doing it at the moment is defining the variables I want to be able to use as global and then adding them into the top of the filtering function.
Is there any easier way to do this, i.e. by preserving scope when a function call is made or is there such a thing as PHP macros?
Edit: Would it be better to use extract($_GLOBALS); inside my function call instead?
Edit 2:
For anyone that cared. I realised I was over thinking the problem altogether and that instead of using a function I should just use an include, duh! That way I can keep my scope and have my cake too.
Edit: Okay, I've re-read your question and I think I get what you're talking about now:
you want something like this to work:
// myInclude.php
$x = "abc";
// -----------------------
// myRegularFile.php
function doInclude() {
include 'myInclude.php';
}
$x = "A default value";
doInclude();
echo $x; // should be "abc", but actually prints "A default value"
If you are only changing a couple of variables, and you know ahead of time which variables are going to be defined in the include, declare them as global in the doInclude() function.
Alternatively, if each of your includes could define any number of variables, you could put them all into one array:
// myInclude.php
$includedVars['x'] = "abc";
$includedVars['y'] = "def";
// ------------------
// myRegularFile.php
function doInclude() {
global $includedVars;
include 'myInclude.php';
// perhaps filter out any "unexpected" variables here if you want
}
doInclude();
extract($includedVars);
echo $x; // "abc"
echo $y; // "def"
original answer:
this sort of thing is known as "closures" and are being introduced in PHP 5.3
http://steike.com/code/php-closures/
Would it be better to use extract($_GLOBALS); inside my function call instead?
dear lord, no. if you want to access a global variable from inside a function, just use the global keyword. eg:
$x = "foo";
function wrong() {
echo $x;
}
function right() {
global $x;
echo $x;
}
wrong(); // undefined variable $x
right(); // "foo"
When it comes to configuration options (especially file paths and such) I generally just define them with absolute paths using a define(). Something like:
define('MY_CONFIG_PATH', '/home/jschmoe/myfiles/config.inc.php');
That way they're always globally accessible regardless of scope changes and unless I migrate to a different file structure it's always able to find everything.
If I understand correctly, you have a code along the lines of:
function do_include($foo) {
if (is_valid($foo))
include $foo;
}
do_include(#$_GET['foo']);
One solution (which may or may not be simple, depending on the codebase) is to move the include out in the global scope:
if (is_valid(#$_GET['foo']))
include $_GET['foo'];
Other workarounds exists (like you mentioned: declaring globals, working with the $_GLOBALS array directly, etc), but the advantage of this solution is that you don't have to remember such conventions in all the included files.
Why not return a value from your include and then set the value of the include call to a variable:
config.php
return array(
'foo'=>'bar',
'x'=>23,
'y'=>12
);
script.php
$config = require('config.php');
var_dump($config);
No need to mess up the place with global variables
Is there any easier way to do this, i.e. by preserving scope when a function call is made
You could use:
function doInclude($file, $args = array()) {
extract($args);
include($file);
}
If you don't want to explicitly pass the variables, you could call doInclude with get_defined_vars as argument, eg.:
doInclude('test.template.php', get_defined_vars());
Personally I would prefer to pass an explicit array, rather than use this, but it would work.
You can declare variables within the included file as global, ensuring they have global scope:
//inc.php
global $cfg;
$cfg['foo'] = bar;
//index.php
function get_cfg($cfgFile) {
if (valid_cfg_file($cfgFile)) {
include_once($cfgFile);
}
}
...
get_cfg('inc.php');
echo "cfg[foo]: $cfg[foo]\n";