Solved

a global config variable that can be read from everywhere

Posted on 2014-02-17
7
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Industry Leaders: 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!

 
LVL 110

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 110

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

691 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