Jump to content

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) 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.