Firemankurt Posted March 16, 2011 Share Posted March 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/ Share on other sites More sharing options...
KevinM1 Posted March 16, 2011 Share Posted March 16, 2011 Not unless Core changes state and you need all of the objects that contain a reference to Core to have those changes reflected within. Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/#findComment-1188314 Share on other sites More sharing options...
Firemankurt Posted March 16, 2011 Author Share Posted March 16, 2011 Thanks Nightslyr... That is a good point and something I will have to analyze as I continue with my design. Any other thoughts from members? Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/#findComment-1188426 Share on other sites More sharing options...
ignace Posted March 17, 2011 Share Posted March 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/#findComment-1188578 Share on other sites More sharing options...
KevinM1 Posted March 17, 2011 Share Posted March 17, 2011 Objects are always passed by reference ...can't believe I forgot that. Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/#findComment-1188632 Share on other sites More sharing options...
Firemankurt Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks ignace. So much to remember in such little brain space. Anyone else need a memory upgrade? Quote Link to comment https://forums.phpfreaks.com/topic/230833-by-reference-or-not-by-reference-thats-the-question/#findComment-1192015 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.