SchweppesAle Posted December 21, 2009 Share Posted December 21, 2009 hi, I'm not really sure why the following code is returning a "Call to a member function getBanners() on a non-object" error message. var $_model; function display ($tpl=null) { $this->_model = &$this->getModel(); global $option,$mainframe, $_model; $Banners = $_model->getBanners(); $this->assignRef('Banners', $Banners); parent::dislay($tpl); } if I replace "$Banners = $_model->getBanners();" with "$Banners = $this->_model->getBanners();" it works fine, I'm trying to shorten the code though. Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2009 Share Posted December 21, 2009 You haven't defined $_model() anywhere, only $this->_model(). Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 21, 2009 Share Posted December 21, 2009 Hmm.. "parent::dislay($tpl);" You forgot the 'p'! Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted December 21, 2009 Author Share Posted December 21, 2009 This is the entire class. I thought I was already declaring the value of $_model with "$this->_model = &$this->getModel();" <?php defined('_JEXEC') or die ('restricted access'); jimport('joomla.application.component.view'); class JtbannersViewList extends JView { var $_model; function display ($tpl=null) { $this->_model = &$this->getModel(); global $option,$mainframe, $_model; $Banners = $_model->getBanners(); $this->assignRef('Banners', $Banners); parent::dislay($tpl); } } ?> Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 21, 2009 Share Posted December 21, 2009 $this->assignRef('Banners', $Banners); parent::dislay($tpl); } Why are you ignoring such an obvious mistake? Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2009 Share Posted December 21, 2009 I thought I was already declaring the value of $_model with "$this->_model = &$this->getModel();" Yes, but any variable declared outside of a method must be pre-pended with $this-> within a method to access it. Also, using the global keyword within a class (or most anytime really) breaks your encapsulation. You may as well not bother using classes or functions at all. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted December 21, 2009 Author Share Posted December 21, 2009 I thought I was already declaring the value of $_model with "$this->_model = &$this->getModel();" Yes, but any variable declared outside of a method must be pre-pended with $this-> within a method to access it. Also, using the global keyword within a class (or most anytime really) breaks your encapsulation. You may as well not bother using classes or functions at all. Good point.I just realized the manual wasn't referring to classes. http://php.net/manual/en/language.variables.scope.php This works though, probably much simpler too defined('_JEXEC') or die ('restricted access'); jimport('joomla.application.component.view'); class JtbannersViewList extends JView { function display($tpl=null) { $model = &$this->getModel(); $Banners = $model->getBanners(); $this->assignRef('Banners', $Banners); parent::display($tpl); } } 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.