PHP Global Variables Issue - php

I've got a scope problem here. and no idea why its not working, ive got a setup as follows:
functions.php
global $id;
$id = $_GET['id'];
index.php
require_once('functions.php');
echo $id;
now inside functions.php i can echo out $id. however my echo $id; inside index.php is bringing up blank. absolutely nothing.
what am i doing wrong?

In PHP, the global keyword allows you to reference variables in the global scope from inside a local scope - eg to access a global variable inside a function. You don't need global in your example, because you are in the global scope anyway.
I suspect you are showing us a simplified version of what you have, where the issue is in code you haven't shown us.
Why you shouldn't use globals
Confusion like this is part of why using globals is a bad idea and should be avoided.
The alternative is to pass variables around explicitly, so for example if you call a function or instantiate a class from another file, you pass the variable in as a parameter to that function or constructor. Doing this, instead of using global variables, makes it easier to follow what function is accessing what variable because you can follow the trail easier.

You don't need globals between files, only for functions.
Functions.php
<?php
$foobar = "Hello";
?>
Index.php
<?php
include('Functions.php');
echo $foobar;
?>

You shouldn't use globals, but you have it backwards. You declare the variable global after you include its definition:
file1.php:
$name = 'Josh';
file2.php:
require_once('file1.php');
global $name;
echo $name;

#thomasrutter is correct (+1) Global variables areA Bad Thing. Always seek alternatives.
Perhaps you can use $_SESSION (which sort of amounts to the same thing, I know), or declare a class which has a static variable and use a getter() and setter() ? (the latter uis definitely cleaner, but $_SESSION might tie in better with your design, I can't say)
Btw, I hope that functions.php was just an example name, or that you have an extermely simple project.
Otherwise fucntions.php is going to get extermaly large and hard to oversee. If you are going OO then user one file per class, otherwse try to group your functions into separate files (file_management.php, databse.php, forms.php and the like).
If you are just starting out, I would advise you to use Netbeans and document your code with PhpDoc comments which will allow you to generate good documentation which you can view in your browser (including the structure of your coed, what gets declared where, used where, descruptions of function parameters and return values, etc)
Btw, I notice that you use include() I prefer require_once. The _once helps spee dperformnce a little and hte require makes sure that you are aware of missing files more quickly.
Oh, and learn to use Xdebug, which plays well with NetBeans.

Related

Why does only one of these PHP variables enter the global scope?

I have PHP files a bit like this
public_html/environment.php
public_html/task.php
phpbin/actions.php
phpbin/library.php
environment.php is included by public_html/* before any other php files are included, phpbin/* assumes everything in environment.php is already available.
It defines these two globals
$g_foo = "...";
$g_bar = "...";
task.php includes this logic
function do_stuff ()
{
require_once determine_required_file ();
...
}
In this case, determine_required_file() returns "/path/to/phpbin/actions.php"
actions.php in turn contains
require_once "/path/to/phpbin/library.php"
Finally, library.php contains
$x = $g_foo;
$y = $g_bar;
I get this error:
Undefined variable: $g_bar;
Now, $g_foo and $g_bar are strictly read-only except in environment.php, I have exhaustively grepped and verified that there are no other places which create or modify variables with these names.
I am aware that PHP globals are weird, and doing things like including files from within functions can mess up your scope. I know that I should probably use define() or some other method, yeah yeah.
My question is this (yes, I'm asking you to speculate in the absence of full code, sorry):
Why might $g_bar generate an error but not $g_foo?
I assume the in-function-inclusion is probably responsible, but assuming these globals really are read-only, what should I be looking for as the culprit for why one ends up in global scope in library.php but not the other?
You need to use include_once instead of required_once for your fields that needed your global variables, or define a global $g_bar, $g_foo.
http://php.net/manual/en/language.variables.scope.php

Global variable isn't accessible inside my Wordpress function. Why not?

I am working in wordpress, and have a function in functions.php. This is meant to set a number of variables based on the context the variable is used in. But there's a problem.
I am using the function in an included template file, and the function is intended to work with variables on the page that template file is included into. I declare all the variables as global inside my function, but the function doesn't recognize the values of the variables. I don't understand why this is happening, because I am certain that the variable scope is being used properly.
To clear up confusion, I have included a simplified code example below, showing the three files involved in this issue. If anybody has any idea why this is happening, I would be delighted to hear it. I am interested in understanding the reasons why it is happening, more than looking for a fix.
functions.php
function set_variables() {
global $data;
print_r($data);
}
included_file.php
set_variables();
(Code that sets other variables and works with HTML)
template_file.php
$data = "Test";
include "included_file.php";
The result of the code above is nothing--I can't get the function in functions.php to recognize the variable defined in template_file.php. However, if I define the $data variable in functions.php, it works.
Like I said, this baffles me since it seems to contradict how declaring global variables within a function is supposed to work. How am I getting it wrong?
It looks like you misspelled the calling function:
set_variable() is not the same as set_variables()
Please note the following from PHP about including files:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
See: http://php.net/manual/en/function.include.php
#zerkms - Thanks very much for answering my question. Turns out all I had to do was declare the variable as global in the file where it was defined.
So, in the example given above, the solution is as follows:
functions.php
function set_variables() {
global $data;
print_r($data);
}
included_file.php
set_variables();
(Code that sets other variables and works with HTML)
template_file.php
global $data = "Test";
include "included_file.php";
I just assumed that the variable declared in the template_file.php was in the global scope, but I suppose it wasn't. Still a bit fuzzy on the whys, but I know the code worked, and I'm really happy about that.

Best way to setup global variables in oop?

I am having a hard time finding an answer to this, I am sure it's there in front of me but I cant put it together.
//Global Variables
global $GLOBAL;
$GLOBAL['myPluginPath'] = dirname( __FILE__ );
//Main Object
class My_Class {
public function __construct() {
// Initialize Settings
require_once $GLOBAL['myPluginPath'] . '/settings.php';
$My_Settings = new My_Settings();
}
}
I want to use a variable so my code feels more organized, I feel I can read my code better when paths are defined this way and it makes it easier to change the variables so that it applys throughout the whole code if needed.
I can get the variables to work by writing this inside my methods.
public function WhatEverFunc() {
global $GLOBAL
require_once $GLOBAL['myPluginPath'] . '/settings.php';
}
The primary question here, I am wondering if this is bad practice, if not is there a better way then having to define global $GLOBAL inside each method. If however it is bad practice can you show me good practice?
There is a one other thing I am really curious about. Inside the main __construct you see I don't use global $GLOBAL because it works without it, but inside that require_once file is another class which has methods that must use global $GLOBAL inside of them. Hope someone can explain that.
Some are saying this is bad practice, I think. I read there is a singleton pattern(bad practice) and a global pattern. Not sure what I did above constitutes, I am just a little lost from what I should do here with what I am trying to achieve.
An object really should have everything needed for it to perform any function it is designed to. If not, the data should be passed via a param to it.
Part of the whole point of objects versus procedural code is that objects can be re-used over and over and in many places.
Lets use this example of why using Globals and OOP is a bad idea:
Lets say you have a great front-end bit of code, and you write it all through objects. Sometime down the track, you need to create additional reporting on the data in your site. You start to code and realize that you can re-use your classes from the front to achieve almost everything you need - but alas your code contains numerous references to globals that are only visible in the front end if called from a certain script.
That's basically just made your objects rather un-reusable.
Although it's probably not the best practise, but I often write a quick class that grabs a hold of various $GET, $_POST and other variables which might be considered "Globals" such as URL params and the like, then in my code, I either pass this information directly to the objects/functions as needed or indeed pass the entire object (rarely).
Using this approach, I am always perfectly ready to re-use objects knowing EXACTLY what they need to function as they are all required in params.
Why don't you use $GLOBALS? You don't even need to use global keyword.
The primary question here, I am wondering if this is bad practice
Well, I believe most people would say having a global state is bad practice because it is harder to manage or doing unit test. Most people would suggest you to use "dependency injection" which instead of depend on global state, you need to inject what the class need directly.
From your code it seems that you're just looking for a way to resolve your class names into file names. For that purpose it's better to use auto loaders. It takes the formality of how your classes map to file names away from the bulk of your code so that you can concentrate on actual development.
Organizing your plugins can be done by using namespaces.
Second, in most cases you don't want to instantiate classes inside the constructor because it leads to coupling, and too much coupling can stifle your project later; instead, inject the dependency:
class My_Class
{
private $settings;
public function __construct(My_Settings $settings) {
$this->settings = $settings;
}
}
To call it:
$c = new My_Class(new My_Settings());
Use this example to solve your problem
The global keyword doesn't work the way you think it does. It does not make a variable have global scope. All it does is specify to the function that you call it in that you want to use the variable in the outer scope.
For example:
$a="test";
function something() {
global $a;
echo $a; //Outputs: test
}
If you want to make a variable global so that it can be accessed from within a class, you need to use the $GLOBALS superglobal.

Retrieving variables from included files

I have a php file that includes another one using include()
I defined a variable $something in the included file and that will change depending on a function that runs in the included file.
Now, I want to print that variable in the original file, when I use echo $something it is printing absolutely nothing, help?
Let's just leave aside that this is a poor design choice for a moment :)
You're probably running into a issue where you haven't declared the variable as global inside the function which modifies it.
function foo()
{
global $something;
$something='bar';
}
You will find the PHP manual page on variable scope most educational in this regard!
So why is this a poor design choice? First of all, check out "Are global variables bad?" which answers the question for C++. The answer is really no different for PHP - it can lead to unmaintainable and unreadable code.
There's another (increasingly historical) wrinkle with PHP though - if the 'register_globals' setting is on, a user can set global variables via the URL query string. This can lead to all manner of security problems, which is why this is now turned off by default (never write new code which requires it to be on).
As a wise man once said, "globals are the path to the dark side. globals lead to anger. anger leads to hate. hate leads to suffering" :)
It is possible you have declared your variable in global scope and are trying to use it in functional scope. To get around this use the global command.
$myglobal = 3;
function printMyGlobal() {
global $myglobal; // will not work without this line
echo $myglobal;
}
Use get_defined_vars to debug defined variables

PHP variable from other file comes back as NULL

In one PHP file, I have this code:
require_once $_SERVER['DOCUMENT_ROOT'] . '/custom/functions.php';
global $testVar;
var_dump($testVar);
In the functions.php file, I have this at the beginning, followed by a few other functions:
function pr($s) {
echo '<pre>', htmlspecialchars(print_r($s,true)), '</pre>';
}
$testVar = 'hello world';
When running the first file, the variable comes back as NULL. I added the global bit but it shouldn't be necessary. This is part of a Joomla module but I've never had problems including files before, it should just work like regular PHP. Why might this be happening?
First, try to use Joomla's path constants such as JPATH_BASE instead of $_SERVER['DOCUMENT_ROOT']. Joomla has a lot of useful constants, check it's documentation.
I've read your answer, and reading php documentation I tried to find a reason to why you need to use global keyword twice.
First, Variable scope.
The scope of a variable is the context within which it is defined. For the most
part all PHP variables only have a single scope.
(...)
However, within user-defined functions a local function scope is introduced.
Any variable used inside a function is by default limited to the local
function scope.
The variable isn't in a function scope, so that's why we thought the NULL was a strange behavior.
But then I read include and found something interesting:
(...)
Any variables available at that line in the calling file will be available
within the called file, from that point forward. However, all **functions**
and **classes** defined in the included file have the global scope.
I can't see any mention about the variables being global in this paragraph. So,it seens that, being cumbersome or not, your solution is the right thing to do when you want to use global variables like that.
In your situation, if doing this is cumbersome, I would create a simple class. If you have just helper functions in your file, create a class Util{} with a lot of methods and $testVar as an attribute.
I have found a solution that seems to work: using the global keyword both when setting the variable initially, and just before I need to use it.
(However this is quite cumbersome, and I'm still not sure why it happens, so if anyone has a better solution, feel free to post.)

Categories