Answer the question
In order to leave comments, you need to log in
How to call a public method in php through object literals?
class A {
public function a() {
echo "a";
}
}
class B extends A {
public function controll($method) {
$methods = [
"returnA" => $this -> a()
]; return $methods[$method];
}
}
$b = new B;
$b -> controll("returnA");
Answer the question
In order to leave comments, you need to log in
you can use __invoke()
public function __invoke($method)
{
$methods = get_class_methods(static::class);
if (!in_array($method, $methods)) {
throw new BadMethodCallException($method);
}
return [$this, $method]();
}
If I understand you correctly, you can do something like this
<?php
class A
{
public function a()
{
echo "a";
}
}
class B extends A
{
public function controll($method)
{
if (method_exists($this, $method)) { // проверяем существует ли метод
$reflectionMethod = new ReflectionMethod($this, $method);
if ($reflectionMethod->isPublic()) { // проверка является ли метод публичным
return $this->$method(); // вызываем функцию
}
}
}
}
$b = new B;
$b->controll("a");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question