Reputation: 2757
This night I have a problem. I would call a method from a class doing like this:
function callMethod($method) {
$class = new Class();
$class->$method;
}
callMethod('Mymethodname()');
but it tells me:
Notice: Undefined property: Class::$Mymethodname();
Has someone solution for this?
Upvotes: 0
Views: 61
Reputation: 57124
have you tried $class->$method();
?
but you should check if $method is a valid and callable function name before you try to call it.
if (method_exists($class, $method)) $class->$method();
Upvotes: 3
Reputation: 2983
its simple, you forgot the parantheses:
$class->$method;
should be
$class->$method();
Also, you should check if said method is available:
<?php
function callMethod($name) {
$class = new Class();
if(method_exists($class, $name)) { return $class->$name(); }
else return null;
}
?>
Upvotes: 2