0

I recently begun to use classes. I've been using procedural programming for quite a while so it has been a bit challenging.

My question.

If I have a class like this:

class Example {

   public $name;
   public $whatever;
   public $yearAdded

   public function __construct($name, $whatever=NULL, $dateAdded)
   {

   some trival code here;

   }


}

How can I make $yearAdded use a global variable I set up somewhere else in another script?

FOR EXAMPLE:

global $currentYear = date('Y');

Would I have to do this way

new example($name, $whatever, $currentTime);

or is there a way to specify within the class to always use $currentYear for $yearAdded.

2 Answers 2

2

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.

http://www.php.net/manual/en/reserved.variables.globals.php

This is not considered to be the best OOP way of doing things, but will get the job done.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you think that it is best in your experience to use these superglobals for something like a current timestamp or is it best to keep that kind of information specific to a class. I know the example I provided is a bit abstract.
what would be consider best OOP way of doing so?
@Hugo, The best thing to do would be to build classes so that they are generic in a way where it makes sense to pass them anything they need to function in a constructor. Then, for each function, pass anything in addition as parameters to them. The only time I break this rule is for things like a database connection, or configuration options. Purists would say even those should be passed as well, and they are right.
0

You already have a builtin time() function, so why do you need a home-grown global variable in your constructor?

If you think carefully about how you are using globals, you'll find out that there are usually better ways of accessing the same info -- e.g. system environment variables, configuration files, etc.

5 Comments

the reasoning is that almost everything that happens in the system gets a time added to the DB. Example: Job gets posted to the system, in the db I have a table with a field called dateAdded which contains 2011-10-01 12:22:00 (NOW). But the database is on a different time zone than the user for example, so I have to account for time zones. So I set a global variable for that user which has their current time and date. Not sure if that's the optimal way of doing it... so I'm open to suggestions!
The time() function yields unix time that is universal (UTC). If you look at Date(), then calling year with the o format flag gives you a UTC year that is also time-zone independent. See [link]php.net/manual/en/function.date.php.
@rsj, You won't be able to guarantee that the clocks are in sync for the DB server and the web server. Even if they're all using NTP, errors can happen.
Sure, and how will you keep the global variables in synch on the different servers scattered around the world? Perhaps you can re-invent NTP :P
sorry to get snarky. But coordination problems among multiple web servers accessing the same db doesn't really have anything to do with the use of global variables in PHP -- unless you are suggesting some network protocol for synchronizing each server's copy of the global variable, At which point, use a central time server seems like a good way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.