I'm trying to dynamically create properties of my test case derivative class inside a setUp
method, but I'm having a terrible time getting it working.
$folder = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes';
foreach ( scandir($folder) as $filename ) {
$path = $folder . DIRECTORY_SEPARATOR . $filename;
if ( is_file( $path ) ) {
$nameExtSplit = explode('.', $filename);
$namePrefixSplit = explode('Hf', $nameExtSplit[0]);
$propertyName = 'Mock'.$namePrefixSplit[1];
// $propertyName is getting set correctly—I've checked
$this->{$propertyName} = $this->makeMock($nameExtSplit[0]);
}
}
I've tried $this->$propertyName
, $this->{$propertyName}
, and $this[$propertyName]
, all to no avail.
I found this question, but he's dynamically setting properties instead of creating and setting them.
How can I dynamically create properties within the object?
EDIT: More details:
This code is inside a class that extends the PHPUnit TestCase class and from which all my test classes are derived. I'm trying to autoload a mock for each one of my classes into protected properties of my custom TestCase class. I'm trying to make it so I don't need to edit my TestCase to have a mock available for a newly created class.