Calling an instance of a class from itself. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Calling an instance of a class from itself. (/showthread.php?tid=10444) |
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 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. Now normally I would have just done: Code: for($i=0; $i < $size; $i++){ 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... <?php Code: class MatchingNode{ 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{ 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(); 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); & 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 |