Solved

a global config variable that can be read from everywhere

Posted on 2014-02-17
7
336 Views
Last Modified: 2014-02-23
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
Comment
Question by:rgb192
  • 3
  • 3
7 Comments
 
LVL 1

Assisted Solution

by:Lacca
Lacca earned 125 total points
ID: 39867030
You can define global a variable like this:

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


Regards, Lacca
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 39867131
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
 

Author Comment

by:rgb192
ID: 39868703
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 39868738
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
 

Author Comment

by:rgb192
ID: 39872143
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 375 total points
ID: 39873538
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
 

Author Closing Comment

by:rgb192
ID: 39881153
thanks for teaching me that a variable can have different values inside functions, classes
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question