Hall of Famer Posted March 19, 2012 Share Posted March 19, 2012 Well I am encountering this dilemma at the moment. The project I am working on has database instantiated with PDO like this: $dsn = "mysql:host={$dbhost};dbname={$dbname}"; $db = new PDO($dsn, $dbuser, $dbpass); But there is a problem with variable scope in functions. Whenever I call a function, the database object $db is not carried over unless it is defined as global variable or in superglobal array like this: (replacing $GLOBALS['db'] with $db will give an error, saying its not an object) $GLOBALS['db'] = $db; function myfunc($query){ $stmt = $GLOBALS['db']->prepare($query); $stmt->execute(); } And then I get the problem of global variables. They are not secure and require lots of memories to store, definitely not a good practice for a project that is ever expanding. However, I cannot think of an easier way of rewriting the code without making it more complex and messy to read. Sure I can instantiate the database object in every function call, but this means the object is redefined again and again if I call multiple functions at the same time. Perhaps I can resolve this problem by completely rewriting the codes to be fully Object oriented, but that will take weeks or even months. Can anyone of you think of a better way of passing database object variable to each individual function? Please help. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/ Share on other sites More sharing options...
trq Posted March 19, 2012 Share Posted March 19, 2012 There is no "better way", if your functions require a database connection, you need to pass it to them. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1328995 Share on other sites More sharing options...
KevinM1 Posted March 19, 2012 Share Posted March 19, 2012 Indeed, that's what the argument list is for. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329100 Share on other sites More sharing options...
Hall of Famer Posted March 19, 2012 Author Share Posted March 19, 2012 So you are saying that registering globals for database information actually is a good practice? I thought using global variables is always bad unless you work on an extremely small project. And if not, I have to pass the database info in all my functions like this? function func1($db, $query){ $stmt = $db->prepare($query); $stmt->execute(); } function func2($db, $query2){ $stmt = $db->exec($query2); } function func3($db, $query3){ $stmt = $db->prepare($query3); $stmt->execute(); $obj = $stmt->fetchObject(); return $obj; } And there is no other way to improve the script? Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329152 Share on other sites More sharing options...
KevinM1 Posted March 19, 2012 Share Posted March 19, 2012 No, thorpe is saying to never use globals at all, and to pass your db into the functions via their argument lists, like you did in the code above. The same general approach would be used in OOP as well. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329170 Share on other sites More sharing options...
Hall of Famer Posted March 19, 2012 Author Share Posted March 19, 2012 No, thorpe is saying to never use globals at all, and to pass your db into the functions via their argument lists, like you did in the code above. The same general approach would be used in OOP as well. I see, so the problem still occurs even after the conversion into fully object-oriented codes is complete? Oh man what a pain, but I get what you are saying now. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329176 Share on other sites More sharing options...
KevinM1 Posted March 19, 2012 Share Posted March 19, 2012 I wouldn't consider that a problem. Scope is a necessary part of programming, especially OOP, where objects are supposed to have hard boundaries. But, even in a procedural environment, scope and encapsulation allow you to create modular code through functions. The thing to remember is that if your code has an external dependency, like a function that has to use a db connection, you need to pass that dependency to the code that uses it in an explicit way. The way PHP (and other languages) do it is via the argument list. In OOP it's actually a bit easier, as you only need to inject the dependency in once, either right when you need it, or when the object is created. You can store the dependency as an object member, and use it whenever you want. And, since objects are always passed by reference, the dependency object can be passed to others with little overhead. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329197 Share on other sites More sharing options...
Hall of Famer Posted March 21, 2012 Author Share Posted March 21, 2012 I wouldn't consider that a problem. Scope is a necessary part of programming, especially OOP, where objects are supposed to have hard boundaries. But, even in a procedural environment, scope and encapsulation allow you to create modular code through functions. The thing to remember is that if your code has an external dependency, like a function that has to use a db connection, you need to pass that dependency to the code that uses it in an explicit way. The way PHP (and other languages) do it is via the argument list. In OOP it's actually a bit easier, as you only need to inject the dependency in once, either right when you need it, or when the object is created. You can store the dependency as an object member, and use it whenever you want. And, since objects are always passed by reference, the dependency object can be passed to others with little overhead. umm dependency injection? Mind explaining a bit about that? Thx. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329681 Share on other sites More sharing options...
trq Posted March 21, 2012 Share Posted March 21, 2012 There is some docs in my framework that might help shed some light on the subject: http://proemframework.org/docs/services-component.html, but there are plenty of other resources around as well. Symfony has a good description in there docs too. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329689 Share on other sites More sharing options...
KevinM1 Posted March 21, 2012 Share Posted March 21, 2012 Symfony has a good description in there docs too. http://symfony.com/doc/current/components/dependency_injection.html Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329799 Share on other sites More sharing options...
trq Posted March 21, 2012 Share Posted March 21, 2012 Symfony has a good description in there docs too. http://symfony.com/doc/current/components/dependency_injection.html Damn it man, I'm trying to point as many people toward my framework as I possibly can without looking like I'm spamming the place Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329802 Share on other sites More sharing options...
KevinM1 Posted March 21, 2012 Share Posted March 21, 2012 Symfony has a good description in there docs too. http://symfony.com/doc/current/components/dependency_injection.html Damn it man, I'm trying to point as many people toward my framework as I possibly can without looking like I'm spamming the place Ah, shit. Quote Link to comment https://forums.phpfreaks.com/topic/259250-about-php-globals-and-database/#findComment-1329836 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.