Re: Returning objects from a recursive function

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

 



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