Jump to content

[SOLVED] call_user_func_array not setting objects


droberts

Recommended Posts

I have a method of a class which should assign an instance variable to that object:

class testklass
{
  public function hello()
  {
    $this->amihere = 'assigned in class';
  }
}

 

This correctly assigns the amihere variable:

$k1 = new testklass();
$k1->hello();
print_r($k1); // Object ( [amihere] => assigned in class )

 

This does not:

$name = 'testklass';
$k2 = new $controller();
call_user_func_array(array($k2 , 'hello'),null);
print_r($k2); // Object ( ) 

 

Is there a way around this?  Thanks in advance!

Sorry for the confusion, I'm not in front of my php code and I mis-typed:

$controller = 'testklass';
$k2 = new $controller();
call_user_func_array(array($k2 , 'hello'),null);
print_r($k2); // Object ( )

$controller is the variable, not $name, and this code indeed does run, but the instance variable is not being set from the method call

<?php
class testklass
{
  public function hello()
  {
    $this->amihere = 'assigned in class';
  }
}

$controller = 'testklass';
$k2 = new $controller();
call_user_func_array(array($k2 , 'hello'),array());
print_r($k2); // Object ( )

 

Works for me.  Version:

PHP 5.3.0alpha3 (cli) (built: Jan  3 2009 23:19:19) 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.