Jump to content

Pass reference into class


svivian

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/132831-pass-reference-into-class/
Share on other sites

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 :)

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.

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.

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! :-\

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)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.