Re: problem with class

From: Date: Fri, 25 Apr 2003 16:03:39 +0000
Subject: Re: problem with class
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-145288@lists.php.net to get a copy of this message
There is an easier solution :) It depends on the version of PHP you are using. I believe if you are using PHP 4.2 and above the following will work. PHP has a built in array called $_GLOBALS when you: global $MyVar; $MyVar = "Wookie"; PHP adds it to the Global array so the following are both the same. function MyFunction() { global $MyVar; echo "$MyVar <br />"; echo $GLOBALS["MyVar"]."<br />; } Should print: Wookie Wookie So instead of having to global all your variables at the starting point and inside the functions you want to use them in you can: $GLOBALS["conf"] = $conf; Then just use $_GLOBALS inside your functions and no more double calling globals. But this only works with PHP 4.2 and above I beleive. http://www.php.net/manual/en/language.variables.predefined.php - explains all the predefined variables in PHP. Hope this helped. Jonathan Pitcher On Friday, April 25, 2003, at 10:47 AM, Prince wrote:
ok.. i got this to work... in db.lib: require("db.set"); //start class class db{ var $_db="";//class wide var //cant define global $conf here gives parse error function echo(){ //now define global conf.. works as long as its inside a //function name global $conf; //echo it now echo $conf;//out put is Array Array???? why twice though echo $conf["date"]."\n";//output is Apr-25-2003 Apr-25- //2003?? why twice echo $conf["log"]."\n";//same output is $conf["date"] (date //value expected but not twice) }} so i guess that means i should make a globals() function that declares all the vars i might need as global that way all i have to do is do globals(); whenever i want to make a global var??? other than the printing the value of $conf or any of the elements in it twice when i only wanted it echoed once is how come i am being told i can define class wide globals like: class whatever { global $var; ... } but it doesnt work for me.. instead i have to define the global use of vars in a function like this: class whatever{ //class wide vars here var $_hahahaha; //function now function ThisIsDifferent(){ //now global vars global $ThisRuleIsStrict; //function vars now $nothing="anything"; //do something with them }} sorry bout the var/functions names but just pulled something out of my head for simple examples but anyways why would i have to put the global keyword only in a function definition?? suppose i can use it that way just make a globals function for it unless we can figure out why i cant class wide them... (oh and get rid of the annoying printing of the values/var type twice when only echoed once)... ----- Original Message ----- From: "Jonathan Pitcher" <jpitcher@cfedc.com> To: "Prince" <friendly@kaltelnet.net> Cc: <php-general@lists.php.net> Sent: Friday, April 25, 2003 11:02 AM Subject: Re: [PHP] problem with class
I would try two things to further test your code. 1,) in db.set I would add the following line before setting any of $Conf's values. global $conf; 2.) in function Echo I would add a third line to make sure the function is being called. echo "Echo Called<br />"; Jonathan Pitcher On Friday, April 25, 2003, at 09:55 AM, Prince wrote:
ok took the [] out of the $conf array and tried it again but for some strange reason when i try to echo them it doesnt really work... all i get is a blank screen when i know for a fact that something is in the elements of that array... ----- Original Message ----- From: "Jonathan Pitcher" <jpitcher@cfedc.com> To: "Prince" <friendly@kaltelnet.net> Cc: <php-general@lists.php.net> Sent: Friday, April 25, 2003 10:33 AM Subject: Re: [PHP] problem with class
When you want to global an array do it the same way you do a variable. global $conf; // GOOD global $conf[]; // BAD remove the [] from your global line and you should be good to go. Jonathan Pitcher On Friday, April 25, 2003, at 09:38 AM, Prince wrote:
hi.. i have in an include file this: ##db.set## //variable used in other files $conf["date"]=date("M-d-Y"); $conf["log"]=date("M-d-y"); //...... ##end## now in the class file i have this: ##db.lib### require("db.lib"); classdb{ var $_db=""; global $conf[];//make the $conf array global now..worked //before but doesnt work now //now try to use it function Echo(){ echo $conf["date"]; echo $conf; }} ###end### now on output all i get for an error was parse error expecting t_old_function t_function t_var or } in db.lib on line 8 and for some reason it comes back to that $conf array in the other file... if i take out the global $conf[]; statement out of the class and put it just under the require statement or if i put it inside a function statement it will make the error go away but when i try to echo them it comes up with a blank screen what is wrong with it.. i did this once before and it worked but dont know why this time its not working... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#145288) next »