0

I'm having a problem creating webservices through nuSOAP (although i believe my problem has nothing to do with it)

What i'm trying to do:

function loadActiveItems() {
    $list = Item::loadActive();
    $ret = array();
    foreach ($list as $val){
        //two tests to check if i really have an object and if the toDTO method is callable
        echo var_dump($val);
        echo is_callable(array($val, 'toDTO'));
        array_push($ret, $val->toDTO());
    }
    unset($val);
    return $ret;
}

I'm getting the following error:

Call to a member function toDTO() on a non-object

and both var_dump($val) and is_callable are returning the expected (the object and true, respectively) from what i've been seeing online, it appears i have a out of scope problem... but for some reason i don't seem to get my head around it :P

Thanks in advance

EDIT: well just check that apparently i don't understand is_callable either because i always get 1 as the result... EDIT2: i'm using php-activerecord if that helps in any way

Atekon
  • 43
  • 7
  • 2
    You should not echo the result of var_dump(). Could you post the result of the var_dump? – Florent Jul 02 '12 at 15:42
  • maybe try `foreach($list as &$val) { ... }` – T I Jul 02 '12 at 15:44
  • @Florent `object(Item)[25] public 'errors' => null private 'attributes' (ActiveRecord\Model) => array (size=5) 'id' => int 1 'itemtype_id' => int 1 'parent_id' => null 'name' => string 'item1' (length=5) 'active' => int 1 private '__dirty' (ActiveRecord\Model) => array (size=0) empty private '__readonly' (ActiveRecord\Model) => boolean false private '__relationships' (ActiveRecord\Model) => array (size=0) empty private '__new_record' (ActiveRecord\Model) => boolean false ` – Atekon Jul 02 '12 at 15:48
  • @Greg nope... it ain't static =( public function toDTO(){ require_once 'DTO/ItemDTO.php'; return new ItemDTO($this->id, $this->name, null, $this->itemtype->name, $this->parent->toDTO()); } – Atekon Jul 02 '12 at 15:49
  • @TomIngram didn't work either =/ Thanks anyway! – Atekon Jul 02 '12 at 15:50

3 Answers3

0

toDTO() may be undefined in your class Item.

Another reason may be that the method isn't public or as @Grep said` static.

Florent
  • 12,310
  • 10
  • 49
  • 58
0

This error never happens on an object that defines the method but it is static or protected/private:

Call to a member function toDTO() on a non-object

That error only happens if $val is not an object. Usually a NULL, FALSE or other scalar.

It's usually a FALSE when the object came for a db_fetch() function but the fetch or the query before it failed.

It's usually a NULL when you have an array that may have NULLs in it.

var_dump($list) and see what's in there and if there are any NULLs. Also change your foreach to have a $key and var_dump($key) as well to see which key is dumped last before the error is issued.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • the list doesn't have any NULL values and the error occurs on the first item ($key = 0) =/ – Atekon Jul 02 '12 at 16:14
  • What's the value on that key? – Mihai Stancu Jul 02 '12 at 17:38
  • `object(Item)[25] public 'errors' => null private 'attributes' (ActiveRecord\Model) => array (size=5) 'id' => int 1 'itemtype_id' => int 1 'parent_id' => null 'name' => string 'item1' (length=5) 'active' => int 1 private '__dirty' (ActiveRecord\Model) => array (size=0) empty private '__readonly' (ActiveRecord\Model) => boolean false private '__relationships' (ActiveRecord\Model) => array (size=0) empty private '__new_record' (ActiveRecord\Model) => boolean false` – Atekon Jul 02 '12 at 18:08
  • Print a [debug_backtrace](http://php.net/manual/en/function.debug-backtrace.php) for the error. Maybe the error is not comming from that object itself but from a function call in the stack. – Mihai Stancu Jul 02 '12 at 18:33
  • well, i'm not really an expert in php so i tried to read the debug_backtrace (which is huge XD) i saw no reference to the $val variable, and according to what i've found i should have, right? – Atekon Jul 02 '12 at 19:02
  • well, anyway i also used debug_print_backtrace which is a lil bit less overwhelming :P i got this: >#0 loadActiveItems() #1 call_user_func_array(loadActiveItems, Array ()) called at [C:\xampp\htdocs\ActiveRecordTest\lib\nuSoap\nusoap.php:4089] #2 nusoap_server->invoke_method() called at [C:\xampp\htdocs\ActiveRecordTest\lib\nuSoap\nusoap.php:3718] #3 nusoap_server->service() called at [C:\xampp\htdocs\ActiveRecordTest\WebServices\WebService.php:88] – Atekon Jul 02 '12 at 19:02
  • Ah yes, i remember now, if you use debug_print_backtrace or debug_backtrace it'll print the current scope backtrace. We need the backtrace of the function that caused the error. – Mihai Stancu Jul 02 '12 at 20:02
  • Use this `function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } set_error_handler("exception_error_handler");` it makes all your errors behave like exceptions and shows you the error stack trace. – Mihai Stancu Jul 02 '12 at 20:06
  • sorry i took so long... well i just tried to do that and i get no difference in the output... (tried it with this sample [errorexception](http://php.net/manual/en/class.errorexception.php) to check if it has anything to do with my config). I probably should have said i've got xdebug turned on but i can't create breakpoint due to the fact that i'm accessing the web service... but i still got the error message with the call stack – Atekon Jul 03 '12 at 08:36
  • Fatal error: Call to a member function toDTO() on a non-object in C:\xampp\htdocs\ActiveRecordTest\Model\Item.php on line 163 Call Stack # Time Memory Function Location 1 0.0007 359840 {main}( ) ..\WebService.php:0 2 0.0275 4510048 nusoap_server->service( ) ..\WebService.php:89 3 0.0289 4525680 nusoap_server->invoke_method( ) ..\nusoap.php:3718 4 0.0292 4529848 call_user_func_array ( ) ..\nusoap.php:4089 5 0.0292 4529864 loadActiveItems( ) ..\nusoap.php:4089 6 0.0554 4997640 Item->toDTO( ) ..\WebService.php:31 this is the output i get – Atekon Jul 03 '12 at 08:38
0

Okay so i figured out the problem... thanks for all the help! I was calling toDTO of another object inside toDTO... problem was that object could be a null! So a simple if(object==null) solved the problem!

Thanks again!

Atekon
  • 43
  • 7