svivian Posted November 15, 2008 Share Posted November 15, 2008 I have an object that I wish to pass into several classes. Since it is a database class (with query execution functions, etc) I ought to be passing the object by reference. Just wondering, is this the right way to do it? class abc { var $db function abc( &$_db ) { $this->db &= $_db; } } If I've passed the $_db reference in, do I need to use the $= reference as well? Or perhaps it would just be easier to make it a global variable? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 15, 2008 Share Posted November 15, 2008 If you are using php 5 all classes are automatically passed as references... though it does look like you are coding for php 4. If you have any questions about references, write a small script to test it. Change values to see if the original changes Quote Link to comment Share on other sites More sharing options...
jordanwb Posted November 15, 2008 Share Posted November 15, 2008 If you are using php 5 all classes are automatically passed as references... though it does look like you are coding for php 4. Both PHP4 and 5 support having the constructor's name the same as the class name. I like to put the name of the class before the variable, to make it clear what is to be expected. But that's just me. In php 4 I think that classes would be passed by reference. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 16, 2008 Share Posted November 16, 2008 If you are using php 5 all classes are automatically passed as references... though it does look like you are coding for php 4. Both PHP4 and 5 support having the constructor's name the same as the class name. I like to put the name of the class before the variable, to make it clear what is to be expected. But that's just me. In php 4 I think that classes would be passed by reference. ... what? I said nothing about constructor names. I said he was programming for php 4 because of the lack of access modifiers and the use of the var keyword for variables. And no, php 4 does not automatically pass objects by reference, you must explicitly tell them to. Quote Link to comment Share on other sites More sharing options...
svivian Posted November 17, 2008 Author Share Posted November 17, 2008 I am using PHP 5, but I've never gotten round to reading up on how to do classes properly, I'm just using the simpler methods that I learned a while back. So you're saying that I don't need the ampersands at all if I'm using PHP 5? P.S. By "access modifiers" do you mean the public/private keywords like in other languages? I had no idea you could do that in PHP! :-\ Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 17, 2008 Share Posted November 17, 2008 Yes, for example... (in php 5) <?php class AClass { private $variable; // Encapsulation example public function setVariable($setting) { $this->variable = $setting; } public function getVariable() { return $this->variable; } } class BClass { public static function changeA(AClass $a) { // Type hinting and static function example $a->setVariable('The variable has been changed!'); } } $a = new AClass(); $a->setVariable('Initial variable!'); echo $a->getVariable(); // Outputs 'Initial variable!' BClass::changeA($a); // Objects automatically passed-by-reference example echo $a->getVariable(); // Outputs 'The variable has been changed!' echo $a->variable; // Error: variable is private ?> There's a crash course on most of the php class fundamentals (other than abstract, which is also commonly used) Quote Link to comment Share on other sites More sharing options...
svivian Posted November 18, 2008 Author Share Posted November 18, 2008 OK so if I have a class like this: class User { private $db; private $id; private $username; function User( $db, $id ) { $this->db = $db; $this->id = $id; $this->db->query("SELECT ..."); } } Will that pass in and store the $db variable as a reference? Design question: an alternative design would be to keep all SQL queries in the database class, for example a "getUserById" function. The database class would save the result to a User object and return it. Is that a better design? It's a toss-up between keeping the DB functionality all together, or keeping each class's functionality together. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 18, 2008 Share Posted November 18, 2008 Yes, unless you explicitly copy an object with the clone keyword, the = operator and function arguments pass objects by reference (in php 5 of course). And no, application specific queries don't belong in a database class. 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.