1

I try to replace a variable inside my object method:

 $fieldname = "project";

 $test = $page->getTemplate()->getProject(); 

This is my approach:

 $test = $page->getTemplate()->'get'.$fieldname();

But I get the error message

syntax error, unexpected ''get'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

1 Answer 1

4
$test = $page->getTemplate()->{'get' . $fieldname}();

Also, I will mention that although getproject and getProject are the same method, this is not correct for properties.

Sign up to request clarification or add additional context in comments.

2 Comments

Could this be solved like this? $insert = 'get'.ucwords($fieldname); $test = $page->getTemplate()->$insert();
Sure, it could.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.