Jump to content

By reference or not by reference... That's the question


Firemankurt

Recommended Posts

I have not used pass by reference much at all in my designs but I am trying to get more efficient as I learn.

 

Is there any advantage to passing by reference in the model below?

 

<?php
class Core
{
private $_SOMEVALUE; // Number of days till an entry should be removed from database
public function __construct() {
$this->_SOMEVALUE = 1234;
}// End Constuct

public function giveSomeValue() {
return $this->_SOMEVALUE;
}// End addNew()
}// End Class Checklist

class Checklist
{
public $_CORE; // Number of days till an entry should be removed from database
public function __construct(&$CORE_obj) {
$this->_CORE = $CORE_obj;
}// End Constuct

public function test1() {
return $this->_CORE->giveSomeValue();
}// End addNew()
}// End Class Checklist


class Grouplist extends Checklist
{
public function __construct(&$CORE_obj) {
parent::__construct($CORE_obj);
}// End Constuct

public function test2() {
return $this->_CORE->giveSomeValue();
}// End addNew()
}//End Class Grouplist

$CORE_obj = new Core();
$GL = new Grouplist($CORE_obj);
echo '<br />test1='.$GL->test1();
echo '<br />test2='.$GL->test2();
?>

 

The "Core" class in my actual application contains a function "DBquery()" that handles all mysql queries. This keeps me from having to add a global "$mysqli" variable everywhere I use a query and allows for centralized error handling.

Objects are always passed by reference, so you don't need the & unless you are passing some other data type like array and you make changes to the passed variable contents. It's best to avoid references in your code, not all programmers understand them and if you are like me you don't work long at one place.

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.