xRay
xRay

Reputation: 801

Calling a function mixed by a part of its name and a variable

My variable $column returns project.

I would like to run the method getProject() dynamically.

This is what I have tried:

$column = ucfirst($column);
$typeOfTask->get{$column}();

But i get this error:

Notice: Undefined property: App\Entity\TypeOfTask::$get

How can I fix this?

Upvotes: 0

Views: 181

Answers (1)

Bartosz Pachołek
Bartosz Pachołek

Reputation: 1318

In that particular case you can simply use:

$methodName = 'get' . ucfirst($column);
$typeOfTask->$methodName();

In this case if $column == 'project' then it will call method getProject on object $typeOfTask.

Upvotes: 1

Related Questions