Instantiation in __construct() PHP5 [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Instantiation in __construct() PHP5 [SOLVED]

Post by mattcooper »

Hi all,

Can anyone see a bug in this constructor? I'm able to print $this->$model in method __construct(), but it is not available elsewhere within the same object and I am pulling my hair out. I've used this technique before without problems, and it is in fact working in other parts of my app.

Code: Select all

 
        private $uses = array('broker', 'location');
 
        public function __construct($method = 'index')
        {
            global $view;
            $this->view =& $view;
            global $registry;
            $this->registry = $registry;
            parent::__construct();
            if (!router::isAlias($method)) {
                $this->{$method}();
            } else {
                $method = router::connect($method);
                $this->{$method}();
            }
            if (ine($this->uses)) { // isset and not empty
                foreach ($this->uses as $model) {
                    $this->{$model} = new $model();
                    if(!$this->$model instanceof $model) {
                        throw new Exception("Model \"$model\" not found in ". __METHOD__);
                    } else {
                        pr(($this->$model)); // is print_r'd correctly
                    }
                }
            }
        }
 
        public function edit()
        {
            if (!isset($this->data)) {
                // this is a bug workaround
                $this->location = new location(); // shouldn't need to do this
                // get a list of locations
                $this->location->get();
                $this->view->set('locations', $this->registry->data['location']);
            } else {
                // another bug workaround
                pr(($this->broker)); // undefined property $broker
                $this->broker->get();
                $this->view->set('broker', $this->registry->data['broker']['organization']);
                $this->view->set('location', $this->registry->data['location']['name']);
            }
            $this->view->render('contacts');
        }
 
It's bound to be obvious, something my tired eyes are missing <rolls eyes>...

Thanks in advance ;)
Last edited by mattcooper on Tue Jul 01, 2008 6:14 am, edited 1 time in total.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Instantiation in __construct() PHP5 [SOLVED]

Post by mattcooper »

Yes, it was my tired eyes to blame... lines 17 - 26 run after the method requested is run :oops: I think I need a holiday...
Post Reply