Re: Returning objects from a recursive function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well first of all, what I did was pass a new BreadCrumbTrail as a 
parameter to my function, since I needed to return one.  I couldn't 
build it in the recursive function because it'd get new'd each time 
the function was called.  The object's one and only instance 
variable, an array, got built up correctly within the function.  The 
moment I tried to return it outside of the function i.e. $myarray = 
myfunction(new BreadCrumbTrail(), $catid), the $myarray variable did 
not think that it was an object (BreadCrumbTrail), even though it 
should've been.

I understand what you mean about having the recursive function as a 
class member function, and I considered that but opted to try a non-
recursive approach first.  I converted the recursive function into a 
different looping structure - a while loop - within a regular 
function, and then when it was done, I proceeded with the code for 
what would've been my terminal condition in the recursive function, 
and returned the object.  It worked!  Also, as a sidenote, my web 
hosting server is only PHP 4.3.8, not PHP 5.  I haven't read a lot 
on PHP 5, other than I recognize your code sample's constructor 
function.


--- In php-objects@yahoogroups.com, Rajesh Kumar <rajesh@a...> wrote:
> crown_of_life once wrote:
> 
> > I have developed with PHP for roughly a year now but am just 
getting
> >  into using objects.  I know Java well and thus I understand 
objects,
> >  so I thought it would be easy to use in PHP but I seem to be 
having
> >  problems.
> 
> There are quite a number of differences between the two. OOP in 
PHP is
> relatively new, so you can't expect too much.
> 
> > I am using a list of Category objects to build a BreadCrumbTrail 
for
> > an online photo album (i.e. HOME >> Category1 >> Subcategory).  
When
> > I return my BreadCrumbTrail object from a regular function, I can
> > access it just fine.  However, when I return it from my recursive
> > function, which queries the database and works backwards from 
deepest
> > most subcategory all the way back to the HOME link, I keep 
getting
> > "Call to a member function on a non-object" whenever I try to 
access
> > any method or property of the object returned from the recursive
> > function.  This is my theory, anyway, after all the testing I've
> > done.  Viewing my code would probably help the most but then I 
would
> > also have to explain the structure of my database too.  I thought
> > maybe someone could help me if they've ever run into this 
problem.
> > Has anyone tried to return an object from a recursive function 
and
> > able to access all of the object's methods and properties?  Look
> > forward to any help I can get.  Thank you.
> 
> Assuming you don't instantiate your class for every function call, 
> chances are that your object is not being passed to every instance 
of 
> your function when you call it repeatedly with new arguments.
> 
> A hasty solution would be to add "global $object_name" at the 
start of 
> the function. Or a better way would be to wrap your recursive 
function 
> in another class and make the object instance a property of this 
new 
> class, and refer to your object as $this->object_name.
> 
> As is always the case, I can't say anything for sure without 
looking at 
> your code. Here's a snippet that describes what I was talking 
about in 
> the previous paragraph:
> 
> <?php
> 
> class MyClass {
> 
>    private $instance;
> 
>    public function __construct($instance) {
>      $this->instance = $instance;
>    }
> 
>    public function my_recursive_function($args)  {
> 
>      // $instance is available to me via $this->instance
>      $this->instance->some_method();
>      $this->instance->var = 'some_value';
> 
>      $args++;
>      $this->my_recursive_function($args);
> 
>    }
> 
> }
> 
> $instance = new SomeOtherClass;
> $my_class_instance = new MyClass($instance);
> 	
> ?>
> 
> This ensures that your instance $instance will always be available 
to 
> your recursive function.
> 
> It's just a problem of scope. Functions have their own scopes.
> 
> > Rachel
> 
> 
> -- 
> [ Rajesh Kumar ]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux