SJH
December 24, 2008, 12:55pm
1
I want to be able to do the following:
$this->recordClassName = 'Blah';
return new $this->recordClassName($this,$this->data);
I was rather hoping that doing the above would be the equivalent of saying:
return new Blah($this,$this->data);
But it returns the following error:
Fatal error: Class name must be a valid object or a string in [redacted]
How do I get PHP to recognise recordClassName as a string?
I don’t get a problem here:
<?php
class Test{}
class Init{
public $classname;
function __construct(){
$this->classname = "Test";
}
function output(){
var_dump(new $this->classname());
}
}
$init = new Init();
$init->output();
What is output when you echo $this->recordClassName?
SJH
December 24, 2008, 1:08pm
3
Argh, to cut a long story short I was defining the variable after calling parent::__construct() which is why it wasn’t working.
Thanks!
Reflection, you should look into it.
<?php
class Testing
{
public function something ( $class )
{
$rc = new ReflectionClass( $class );
return $rc->newInstance( $this, $this->data );
}
}
$t = new Testing;
$c = $t->something( 'Blah' );