shane18 Posted December 28, 2009 Share Posted December 28, 2009 $DB = new mysqli("localhost","username","password","database"); function blah(){ // blah } how can i globalize that object in the function so i don't gota link back to it every time like this blah($DB) Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 function blah() { global $DB; // blah } If I understand you correctly... this should work.. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 I tried that and get the error: Catchable fatal error: Object of class mysqli could not be converted to string in /home4/finestto/public_html/index.php on line 225 Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 -Deleted- Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 Does it work when you pass DB as a function variable? Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 like blah($DB)? yea it does Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 28, 2009 Share Posted December 28, 2009 how can i globalize that object in the function so i don't gota link back to it every time like this blah($DB) You learn to organize your stuff properly so you don't have to use things like that Seriously, here are things I would do to PHP 6 if it was my decision: 1) Remove the global keyword and remove all the super globals. 2) Stop letting people do stupid stuff like using undefined variables and accessing non-existent array indices, i.e. bump it from a notice to a fatal error. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 28, 2009 Share Posted December 28, 2009 how can i globalize that object in the function so i don't gota link back to it every time like this blah($DB) You learn to organize your stuff properly so you don't have to use things like that Seriously, here are things I would do to PHP 6 if it was my decision: 1) Remove the global keyword and remove all the super globals. 2) Stop letting people do stupid stuff like using undefined variables and accessing non-existent array indices, i.e. bump it from a notice to a fatal error. Introduce strong typing. Require all variables to be declared at the beginning of the function's body.... In no time we'll have Pascal Hypertext Preprocessor Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 Well, the point of this is that I have a function that needs to use the mysqli object i created at the top of the page... and im trying to figure out the best way to do this... Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 28, 2009 Share Posted December 28, 2009 The best way is to explicitly pass it as parameter. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 So global $A, $B, $blahhhh; only works on strings? Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 28, 2009 Share Posted December 28, 2009 Of course not. You must be doing some other mistake if ti doesn't work as expected. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 28, 2009 Share Posted December 28, 2009 You should be happy, not sorry, that the globals aren't working. I promise you, it makes more problems than it solves. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 How do they cause problems? I just wana beable to global $DB and because its an object an error is being created; in a function so that i don't gota pass it as a parameter... Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 So I should pass everything I need to as a parameter instead of using global $blah;??? Quote Link to comment Share on other sites More sharing options...
trq Posted December 28, 2009 Share Posted December 28, 2009 I have a function that needs to use the mysqli object i created at the top of the page... and im trying to figure out the best way to do this... As has been stated, the best way to do this is to pass the object into the function as an argument. Topic resolved, move on with your day. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 28, 2009 Share Posted December 28, 2009 From Wikipedia: They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in protected memory) and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions. The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program. They make separating code into reusable libraries more difficult because many systems (such as DLLs) don't directly support viewing global variables in other modules. They can lead to problems of naming because a global variable makes a name dangerous to use for any other local or object scope variable. A local variable of the same name can shield the global variable from access, again leading to harder to understand code. The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code. As to your problem. I just tried running this code: $mysqli = new mysqli('localhost','root','','test'); function blah() { global $mysqli; var_dump($mysqli); } blah(); And it runs fone, so there must be something else wrong (like another global variable called $DB) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 28, 2009 Share Posted December 28, 2009 How do they cause problems? I just wana beable to global $DB and because its an object an error is being created; in a function so that i don't gota pass it as a parameter... Because it tightly couples things and makes it much easier to screw up and maintain in the long run. That's why I'd rather see it removed from PHP entirely; it's not obvious to the beginner why it's a bad idea. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 $_SESSION, $_POST globals aren't bad right? aren't they required? --Also, thanks Mchl! Quote Link to comment Share on other sites More sharing options...
trq Posted December 28, 2009 Share Posted December 28, 2009 Using any of the super globals within a function or method I would consider bad practice because as Daniel0 has pointed out, your function now relies on an external variable it has no control over. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 I see, i see... thanks alot everyone sorry for all the questions... im trying to become good so i ask alot of questions... been programming for years and i can make alot of things work but i do it poorly and im trying to improve that... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 28, 2009 Share Posted December 28, 2009 $_SESSION, $_POST globals aren't bad right? aren't they required? No, actually they're not required for reading data (though it doesn't support the FILTER_SESSION). See filter_input. As for writing, that shouldn't happen to $_POST (though I see people doing it regardless) and having to write to $_SESSION is a design flaw, though it can be abstracted away using something like Zend_Session or something you've written yourself. Even if you can read POST data using filter_input(), you should still actually pass the data as a parameter to the consumers to ensure portability and decoupling. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 28, 2009 Share Posted December 28, 2009 $_POST and $_GET (and other superglobals) are a bit different, since they're part of language itself. Still, you should be rather careful when toying with them. I had some funny experiences once, when I was rewriting some of my functions that relied on $SERVER variable, to be able to use them in CLI (where there is no $SERVER variable). Since then, I only use them in global scope, and if I need their values in functions, I pass them through parameters. Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 28, 2009 Author Share Posted December 28, 2009 Thanks everyone Quote Link to comment 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.