CodeIgniter Forums
Calling an instance of a class from itself. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Calling an instance of a class from itself. (/thread-10444.html)



Calling an instance of a class from itself. - El Forum - 07-31-2008

[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


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Yash[/eluser]
What do want to ask? approach ?


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Taff[/eluser]
Firstly, thanks for replying...

I basically would like to know how to call a class from itself with codeIgniter...
&lt;?php

Code:
class MatchingNode{

    function MatchingNode($nodeId){
        echo "loaded ".$nodeId;
        $this -> fillChildrenNodes();
    }
    
    function outputSomething(){
        echo "I can be called";
    }
    
    function fillChildrenNodes(){
    //Pseudo array
        $tmp = array(101,123,126,129);
        for($i = 0;$i <= 4;$i++){
            $A1 = &get;_instance();
            $param = $tmp[$i];
            $A1->load->library("MatchingNode",$param);
            //Call a method
            $A1->matchingnode->outputSomething();
        }
}

?&gt;

This is a pseudo code example, but doesn't work, I would like to know how to do something like (similar code without codeigniter):

Code:
class MatchingNode{

    function MatchingNode($nodeId){
        echo "loaded ".$nodeId;
        $this -> fillChildrenNodes();
    }
    
    function outputSomething(){
        echo "I can be called";
    }
    
    function fillChildrenNodes(){
    //Pseudo array
        $tmp = array(101,123,126,129);
        for($i = 0;$i <= 4;$i++){
            $A1 = &get;_instance();
            $param = $tmp[$i];
            $fubar = new MatchingNode($param);
            //Call a method
            $fubar->outputSomething();
        }
}

?&gt;

Cheers,
Taff


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Bramme[/eluser]
Just use $this->outputSomething() in your fillChildrenNodes function...


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Taff[/eluser]
Hey,
that was just some pseudo code to give you an idea.

I want to call a function that has a method in it which contains a loop.

In this loop I would like to create 1 instance of the same class per loop.

So when the class is first called with a parameter (e.g. 10)

Code:
$CI =& get_instance();
$CI->load->library("MatchingNode",10);

It will provide $CI->matchingnode with a number of variables depending on the value of the parameter passed, for example a text description, it's parents, a link, and some other odds and ends which it retrieves from the database.it also looks in the database for everything which is a child of 10 (lets say it has the children 12, 15 & 18).

I now have all this information for $CI->matchingnode including an array of children.

I now want to loop through the array and create a new instance of matchingnode which will do exactly the same...so it will create a new instance along the lines of

Code:
$CI->load->library("MatchingNode",12);
$CI->load->library("MatchingNode",15);

&

Code:
$CI->load->library("MatchingNode",18);

and upon doing so will generate the information I need for these children too...AND it will also check to see if these children have children of their own....and if so, carry on looping.

But how can I call more than one instance of a library item?

I hope someone can understand what I need now.

Thanks,
Taff


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]wiredesignz[/eluser]
Once the library class has been loaded you can instantiate a new Object.
Code:
$MatchingNode18 = new MatchingNode(18);



Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Colin Williams[/eluser]
Yeah, this is plain old PHP. No need to use the Loader class.


Calling an instance of a class from itself. - El Forum - 07-31-2008

[eluser]Taff[/eluser]
Ahhhh, why oh why didn't I try that?

Spent so much time trying to call it as if I'm loading it, that I sort of missed the idea.

Thanks everyone...got some major catching up to do now.

Taff