Welcome Guest, Not a member yet? Register   Sign In
Problem with library - problem with extending base class maybe?
#1

[eluser]LeePR[/eluser]
I'm having a little problem with a custom library. I have a StateCollection class that extends a Collection class. When I load the StateCollection library, the code in the constructor executes, but when I try to call one of the methods (inherited from Collection), I get an error:
Quote:Severity: Notice
Message: Undefined property: CI_Loader::$statecollection
Filename: views/stateList.php
Line Number: 3

Is the code in my Collection/StateCollection class at fault? Here's the code...

Collection (library):
Code:
<?php

    if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Collection {
        public $arrCollection = array();
        public $CI;
        
        public function __construct() {
            // Empty constructor.
        }
        
        public function Collection() {
            $this->__construct();
        }
        
        public function hasNext() {
            // Later.
        }
        
        public function getNext() {
            // Later.
        }
        
        public function getCollection() {
            return $this->arrCollection;
        }
        
        public function add($object) {
            $arrCollection[] = $object;
        }
    }
?>
StateCollection (library):
Code:
<?php

    if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    require_once('Collection.php');
    require_once('State.php');
    
    class StateCollection extends Collection {
    
        public function __construct() {
            parent::__construct();
            $this->CI =& get_instance();
            $this->populate();
        }
        
        public function Collection() {
            $this->__construct();
        }
        
        public function populate() {
            $this->CI->load->database();
            $sql = "SELECT lst_id, lst_code, lst_name " .
                "FROM rsfs_lookup_state";
            $query = $this->CI->db->query($sql);
            
            foreach ($query->result() as $row) {
                $collection[] = new State($row->lst_id);
            }
        }
    }
?>
stateList (view):
Code:
<?php
    $this->load->library('StateCollection');
    $states = $this->statecollection->getCollection(); // Undefined property: CI_Loader::$statecollection
?>
Can anyone offer some guidance?

Cheers,
LeePR
#2

[eluser]LeePR[/eluser]
If I change my view to the following, it still seem to load the library, but chokes on the method call:
Code:
<?php
    // $this->load->library('StateCollection');
    // $states = $this->statecollection->getCollection();
    $statecollection = $this->load->library('StateCollection');
    foreach ($stateCollection as $state) {
        echo $state->getName() . "<br/>";
    }
?&gt;
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: stateCollection
Filename: views/stateList.php
Line Number: 5

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/stateList.php
#3

[eluser]mrkirkland[/eluser]
Did you find an answer?
#4

[eluser]Pygon[/eluser]
Regarding your first post:
__construct shouldn't be declared "public". Also in "StateCollection", you function should be "public function StateCollection", not "Collection".

Regarding your second post:

$stateCollection isn't the same as $statecollection.


--Just the things I noticed off the bat.




Theme © iAndrew 2016 - Forum software by © MyBB