Reputation: 2558
If I create the instance of the class like so
$template = new EmailTemplate(array('templateFile' =>'template__test.php'));
Why does the following echo NULL
?
class EmailTemplate {
private $templateFile;
function __construct($args) {
foreach($args as $key => $val) {
if(property_exists($this, $this->{$key})) {
$this->{$key} = $val;
}
echo $this->templateFile;
}
}
I was expecting the constructor to echo "template__test.php".
Any help?
Upvotes: 0
Views: 405
Reputation: 329
You should call the constructor like this:
class EmailTemplate {
private $templateFile;
function __construct( $args ) {
// Loop through the arguments
foreach ( $args as $key => $val ) {
// Check if the current class has a defined variable
// named as the value of $val
if ( property_exists( $this, $key ) ) {
// If it has, set it to the passed value
$this->$key = $val;
// If you pass multiple array items, you should echo $this->$key;
}
// Print the defined variable to screen
echo $this->templateFile;
}
}
}
Upvotes: 1