jaymc Posted October 22, 2009 Share Posted October 22, 2009 I am using a paypal API which I have tested and works fine I am now integreating it into my website and it is no longer! I have debugged as much as I can and what it has come down to is this There is a function which has function hash_call($methodName,$nvpStr) { //declaring of global variables global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature; None of those variables contain any data when echoed inside the function.. If I echo them outside the function, it works fine So the conclusion but not the solution is that something somewhere is not allowing me to call a global. Is there anything that would cause this? if so I can then check through all the layers of my code for the problem child. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/ Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 Are these variables available in global scope, or perhaps only in a function that calls hash_call()? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942123 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 That's what happens when you pollute the global namespace, and it's in particular why using global variables is bad practice. Great, now I have a case study to show people when I say this Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942124 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 <? $API_UserName="dummy@dummy.com"; $API_Password="12345"; $API_Signature="qwertyqwertyqwety"; // BN Code is only applicable for partners $sBNCode = "PP-ECWizard"; function hash_call($methodName,$nvpStr) { //declaring of global variables global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature; echo $API_UserName; // DOES NOT WORK } echo $API_UserName; // DOES WORK ?> Polluting global namespace? Hmm, how may I have done that? Thats what I am looking for really, the reason that may cause global to lose functionality? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942130 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 It works for me. You're not calling the function in the code you posted though. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942133 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 It works for me. You're not calling the function in the code you posted though. Yes it works for me.. I have a working version of the whole thing But when I simply plug it into my website, the globals stop working I havn't touched the code, simply copy and pasted So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942139 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin Which is precisely why it's bad practice. You have no real control over what writes to it when, and it's a pain to debug. I suppose you can always step through the code execution manually until you figure out what resets it. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942153 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin Which is precisely why it's bad practice. You have no real control over what writes to it when, and it's a pain to debug. Its how paypal have written most of there API's In fairness I can echo the variables immediately before the function call and immediately after the function call Just not inside the function... so I don't think anything is overwriting there values? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942154 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 I don't know how your script looks, but global will only import things from the global namespace, not from the local namespace where the function was called. So something like this will not work: <?php function foo() { global $bar; echo $bar; } function bar() { $bar = 'foo'; foo(); } bar(); Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942162 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2009 Share Posted October 22, 2009 In fairness I can echo the variables immediately before the function call and immediately after the function call OK, show us that code. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942165 Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 Are these variables available in $GLOBALS array? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942168 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 A file called paypal.php <? $API_UserName="seller_1256141718_biz_api1.hotmail.com"; $API_Password="1256141861"; function beees() { global $API_UserName, $API_Password; echo $API_UserName; echo $API_Password; echo "1234567<br><br>"; } ?> a file called index.php require_once("funcs/paypalfunctions.php"); echo "1 $API_UserName<br><br>"; beees(); echo "3 $API_UserName<br><br>"; die(); I get this 1 seller_1256141718_biz_api1.hotmail.com 1234567 3 seller_1256141718_biz_api1.hotmail.com Hence, $API_UserName and $API_PassWord are not being echoed inside the function Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942170 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 Are these variables available in $GLOBALS array? print_r($GLOBALS); gives me this [API_UserName] => [API_Password] => Blank Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942173 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Why exactly is it you don't just pass variables by argument? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942175 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 Why exactly is it you don't just pass variables by argument? Because I dont want to screw with paypals API or attempt to rewrite it, especially as they update it It should work, its tried and test. It is just a case of something conflicting which i must find. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942177 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 Hmm. I have an idea why it may be although no idea why it would in my index file I have this $html->body(cpanel_module_loader(file.php)); cpanel_module_loader("browser") is a function which will open file.php which is what contains all that code I gave you... It will then echo it to screen It seems that because its a case of a function running inside another function.. as obviously if I just call file.php direct it all works fine. Is this a rule of php that global calls do not work if you are inside another function. The function cpanel_module_loader uses globals... is this why? function cpanel_module_loader($file) { global $html, $my, $client, $user; include($file); $file includes other files which have functions which do not appear to have working globals! } Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942181 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2009 Share Posted October 22, 2009 What does a phpinfo(); statement show for register_globals on both the system where it works and on the system where it does not work? [rant] Perhaps someone figured out how to disable the global keyword to stop people from writing bad code that takes hours to troubleshoot, instead of a few minutes. [/rant] Edit: As to your latest post, you do realize that when you don't post your actual code that is exhibiting the symptoms, that NO ONE can help you with what your actual code is doing. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942185 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 Edit: As to your latest post, you do realize that when you don't post your actual code that is exhibiting the symptoms, that NO ONE can help you with what your actual code is doing. I was hoping someone could just say .. Are you running this function inside another function? As no one knows any obvious causes I have stripped out my code to try and find the root culpret Is that the reason then? Because I have functions nested in functions. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942189 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 php_info(); register_globals Off Off Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942190 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 I was hoping someone could just say .. Are you running this function inside another function? Isn't that pretty much a paraphrase of this post? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942194 Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 I was hoping someone could just say .. Are you running this function inside another function? Isn't that pretty much a paraphrase of this post? Or this Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942196 Share on other sites More sharing options...
jaymc Posted October 22, 2009 Author Share Posted October 22, 2009 I was hoping someone could just say .. Are you running this function inside another function? Isn't that pretty much a paraphrase of this post? Didnt see that post! Cool so now I know What a pain What are the ways around it other than using register globals Any other solutions Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942246 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Your by far best option is to pass things by argument. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942247 Share on other sites More sharing options...
KevinM1 Posted October 22, 2009 Share Posted October 22, 2009 Your by far best option is to pass things by argument. Exactly right. What does: $API_UserName = "seller_1256141718_biz_api1.hotmail.com"; $API_Password = "1256141861"; function beees($userName, $userPass) { echo $userName; echo $userPass; echo "1234567<br><br>"; } echo "1 $API_UserName<br><br>"; beees($API_UserName, $API_Password); echo "3 $API_UserName<br><br>"; die(); Give you? Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942250 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2009 Share Posted October 22, 2009 What are the ways around it other than using register globals Register globals would not cause your code to work. However, register_globals being on could have accounted for the program variables being overwritten if you had some post/get/cookie/session variables with the same name as your program variables. Quote Link to comment https://forums.phpfreaks.com/topic/178622-global-not-working/#findComment-942252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.