• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

a global config variable that can be read from everywhere

I have procedural code mixed with object oriented code
Computes my hours and shows emails so I can present to client

so I want to work on code at the same time to make more re-usable

but there are many limitations

I have two separate table names for two companies, because I do not want to mix data.
So I would different php file for each company.
So I have to find and replace the tablename.
Maybe a config variable would be easier.
How to have a config variable that can be in object oriented classes and procedural functions.

I know that globals are bad but my old code is so disorganized.
0
rgb192
Asked:
rgb192
  • 3
  • 3
4 Solutions
 
LaccaCommented:
You can define global a variable like this:

<?php
if (!isset($_SESSION['variable_name']))
         $_SESSION['variable_name'] = <VARIABLE_VALUE>;
?>


Regards, Lacca
0
 
Ray PaseurCommented:
The principal advantage of using PHP sessions is to persist data between pages (requests).
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

This makes a variable "global" in as much as it assigns the value to a superglobal array.

A "config" variable might make sense.  While the global statement is probably a code smell, it's OK to use it if it solves a problem for you.

You might also think about an "ini" file that would let you move your custom data outside of the script.
0
 
rgb192Author Commented:
I am not sure if $_session works in a procedural function and an object oriented class.
I may have to create and read the session in many times in the code.








what is a config variable

I have been asking experts-exchange questions about code where

$config['default_username']
$config['default_password']

and I think these variables can be read throughout the code

but is it just using the word '$config' that makes this possible?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
is it just using the word '$config' that makes this possible?
No, The variable $config has no special meaning in PHP.

PHP variables are case-sensitive, so $_session is not the same as $_SESSION.  The latter is a superglobal.  The term "superglobal" is a term of art in PHP programming.  $_SESSION is available in every namespace, visibility and scope
http://www.php.net/manual/en/language.variables.superglobals.php
0
 
rgb192Author Commented:
The variable $config has no special meaning in PHP.

okay, for this example because I do not want to write many lines of code to start session and read session from every class method, how can I set a global variable one time and be able to read the global variable in every procedural function and object oriented class method on the same page?

Yes I read many times that global is bad but this code is not public and global may be the solution for this example and I will learn why global is bad by using in this example.
0
 
Ray PaseurCommented:
Man page references:
http://www.php.net/manual/en/language.variables.superglobals.php
http://www.php.net/variables.predefined
http://www.php.net/manual/en/reserved.variables.session.php
http://www.php.net/manual/en/ref.session.php

$_SESSION is a superglobal variable in PHP
You start the session with exactly one line of code:

session_start();

Open in new window

Once the script has done that, the $_SESSION array is available to every scope, visibility and namespace.  Every PHP programmer knows that $_SESSION is a superglobal, and as a result, every PHP programmer takes care to use $_SESSION correctly, and to avoid accidentally overwriting its contents.

Man page references:
http://www.php.net/language.variables.scope.php
http://www.php.net/manual/en/language.variables.scope.php

Why are global declarations bad?
They aren't necessarily bad if you're the only one on the project.  But what if you have two programmers working on the project.  One is in New York and one is in Beijing, so they don't talk much, except through forks and commits.  One of the programmers uses a variable inside a function (expecting it to be encapsulated by the function definition) and he calls the variable $xyz.  The other programmer also uses $xyz inside a different function, and he makes it global.  But then another programmer gets involved, and this happens:
http://iconoun.com/demo/global_is_bad.php

There should be nothing that I can do in my part of the project that would cause a run-time failure in your part of the project.  But the use of the global keyword can cause that to happen.  That's why "global is bad."

<?php // demo/global_is_bad.php

/**
 * Question: Why is the use of the global keyword "bad?"
 *
 * http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28367325.html#a39872143
 */
error_reporting(E_ALL);


// THE VALUE CAN BE SET IN THE MAIN SCOPE OF THE SCRIPT
$xyz = 'Cat';
echo PHP_EOL . "IN MAIN SCOPE THE VALUE OF \$xyz IS: ";
var_dump($xyz);

// THE VALUE IN THE MAIN SCOPE OF THE SCRIPT DOES NOT INFLUENCE THE VALUES IN THE FUNCTIONS
NY('Dog');
BJ('Pig');

// A COMPLETELY NEW ANIMAL, NO LONGER ENCAPSULATED, BUT NOW INJECTED INTO THE MAIN SCRIPT!
SF('Octopus');

echo PHP_EOL . "IN MAIN SCOPE THE VALUE OF \$xyz IS: ";
var_dump($xyz);

// WHAT HAPPENS HERE?
NY(42);
BJ();


// SCRIPT FROM PROGRAMMER IN NEW YORK:
function NY($v=FALSE)
{
    // STORES THE VALUE IN THIS NAMED VARIABLE INSIDE THE FUNCTION
    if (!empty($v)) $xyz = $v;
    echo PHP_EOL . "IN NY() THE VALUE OF \$xyz IS: ";
    var_dump($xyz);
}

// SCRIPT FROM PROGRAMMER IN BEIJING
function BJ($v=FALSE)
{
    // PROGRAMMER MAKES THE VARIABLE GLOBAL
    global $xyz;

    // STORES THE VALUE IN THIS NAMED VARIABLE INSIDE THE FUNCTION
    if (!empty($v)) $xyz = $v;
    echo PHP_EOL . "IN BJ() THE VALUE OF \$xyz IS: ";
    var_dump($xyz);
}

// SCRIPT FROM PROGRAMMER IN SAN FRANCISCO
function SF($v=FALSE)
{
    // PROGRAMMER MAKES THE VARIABLE GLOBAL
    global $xyz;

    // STORES THE VALUE IN THIS NAMED VARIABLE INSIDE THE FUNCTION
    if (!empty($v)) $xyz = $v;
    echo PHP_EOL . "IN SF() THE VALUE OF \$xyz IS: ";
    var_dump($xyz);
}

Open in new window

0
 
rgb192Author Commented:
thanks for teaching me that a variable can have different values inside functions, classes
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now