Class name must be a valid object or a string

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?

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' );