Solved

a global config variable that can be read from everywhere

Posted on 2014-02-17
7
327 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 108

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 108

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now