nloding Posted August 18, 2007 Share Posted August 18, 2007 I posted this in the OOP section but decided to move it here because it deals with proper use of globals, just for this example it's within classes; and I wanted a quicker answer (I don't see many people in the OOP subforum ever). I have several classes in individual include files. As an example, we'll use my database class and my user class. They do what their names imply. In short, what is the most proper way to include the database object into the user object? I've seen MANY ways of doing it, but I've also read it's not a good thing to use global objects like that. Should I do this? class Database { function Database { // connect to database } } $db = new Database; class User { global $db; function User { // set $username or whatever } } ... or ... $db = new Database $user = new User($db); Or what? I'm confused ... I should also add that the database class is used in multiple classes in my page (an archive class, a forum class, a security class, and the user class). Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/ Share on other sites More sharing options...
nloding Posted August 18, 2007 Author Share Posted August 18, 2007 To add another layer to this: how does this all fit together in the puzzle of multiple pages in a website? What about having this at the top of every page: include('ultimate.inc') ultimate.inc <?php include('database.inc'); include('user.inc'); $db = new Database; $user = new User; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327757 Share on other sites More sharing options...
dbo Posted August 18, 2007 Share Posted August 18, 2007 I was always taught that you wanted to keep the scope of your variables as small as possible, which also means to use globals as seldom as possible. That being said I've used them on occassion... but I don't think your example is a good use for them. Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327779 Share on other sites More sharing options...
nloding Posted August 18, 2007 Author Share Posted August 18, 2007 Then how would I set that up? Obviously within the forum and user classes I need to call the database class. So what's the proper, most secure way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327804 Share on other sites More sharing options...
dbo Posted August 18, 2007 Share Posted August 18, 2007 You can pass it in as a parameter, you could make your user/forum classes children of it through inheritance, or you could just make it a private member of the class. Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327807 Share on other sites More sharing options...
nloding Posted August 19, 2007 Author Share Posted August 19, 2007 I am still new to PHP, so if I wanted to make it a private member, can you show an example? Same with passing it as a parameter? Just so I have something to build on? Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327822 Share on other sites More sharing options...
dbo Posted August 19, 2007 Share Posted August 19, 2007 Same way you've got it just lose the global keyword in front of it and make sure you include the file that has the database class in the forum and user class files. Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327823 Share on other sites More sharing options...
ToonMariner Posted August 19, 2007 Share Posted August 19, 2007 <?php include('database.inc'); include('user.inc'); $db = new Database; $user = new User; $user->db = $db; ?> then in the user class.... <?php $this->db->query; ?> if you are using php5 9I stringly advise you do if - even if you don't do that much oop. in php 5 you have the __autoload function which will auto include all teh files you need on a class call - have a look in the manual __autoload measn you only include those files you need and non that you don't Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327824 Share on other sites More sharing options...
nloding Posted August 19, 2007 Author Share Posted August 19, 2007 Just the example I needed, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/65615-solved-global-variables-or-not-whats-proper/#findComment-327863 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.