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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.