is it possible to do something like this in php?
$this->getUser$number();
I can't find if there's somekinda syntax to make this possible.
Regards
is it possible to do something like this in php?
$this->getUser$number();
I can't find if there's somekinda syntax to make this possible.
Regards
Yes, there is:
$this->{"getUser".$number}();
Of course you can use the same syntax outside a method:
$obj = new Something();
$obj->{"getUser".$number}();
That said, code like this is a bad smell and should be avoided. For example, it's better to make the getUser
method accept an argument and call it like this:
$this->getUser($number);
This way the call site remains "sane", while the implementation of getUser
can use $number
as e.g. an index into an array and also achieve much better clarity.
It's called OOP programming, it is used in most likely any big system. It is also used alot in a MVC application. Read more about OOP programming here; http://www.php.net//manual/en/language.oop5.php
These expression can also be used like this;
$this->$name
or even further to;
$this->name =& $name
$this->name = $${val}
It is not possible to use symbol $
as name of function. But on the other hand $this->getUserNumber();
you can use inside some class.
You can also do this as
$number = "10";
$func = "getUser".$number ;
$this->$func();
it'll call some function named
getUser10();
Yes you can do this:
$methodName = 'getUser' . $number;
$this->$methodName();