Welcome Guest, Not a member yet? Register   Sign In
Calling an instance of a class from itself.
#1

[eluser]Taff[/eluser]
I am rewriting some code I have inherited and need to get create a tree type class.

Currently the class calls a new instance of itself to create a child "branch" i.e. it recursively creates a full tree. How would I do this in CI? I currently have a Match class that is calling the MatchingNode class like this:

Code:
//Load the node
        $CI =& get_instance();
        $CI->load->library("MatchingNode",$cat);
        $rootNode = $CI->matchingnode->getNodeName();

which works perfectly.

In my MatchingNode.php class I have a function that I will be calling for each child.
It looks like this:
Code:
//Ideally I would like to loop through an array and create a new instance for each child.
    function fillChildrenNodes(){
        $CI = & get_instance();
        $CI->load->model('nodemodel');
        //Get some numbers from the database
        $tmp = $CI->nodemodel->fill_child_nodes($this->nodeId);
        //Returns the array as I would expect
        $this->nodeSize = sizeof($tmp);
        //Loop through the array...
        for($i = 0;$i < $this->nodeSize;$i++){
//THIS IS WHERE THE PROBLEM IS
            $A1 = &get;_instance();
            $param = $tmp[0]['KAT_ID'];
            //Echos out correctly
            echo "<br />Child: ".$param;
            //Reload the library passing it a different parameter...no error
            $A1->load->library("MatchingNode",$param);
            //I should have received an output loading...file_id for each child.
        }
    }

Now normally I would have just done:
Code:
for($i=0; $i < $size; $i++){
            $row = mysql_fetch_assoc($result);
//The old fashioned way
            $nextChild = new matchingNode($row[KAT_ID]);
            $nextChild -> setNodeName($row[KAT_NAME]);
            $nextChild -> setParentNodeId($this -> nodeId);
            array_push($this -> childrenNodes, $nextChild);
            echo $this->childrenNodes;
        }

Can anyone point me in the right direction?

Thanks,
Taff


Messages In This Thread
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 05:26 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 05:30 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 06:36 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 06:57 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 07:42 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 07:46 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 07:55 AM
Calling an instance of a class from itself. - by El Forum - 07-31-2008, 09:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB