I would like to call an other method in from a method in my class, but i don't know how :/
I have an array: ['header', 'navbar', null, 'footer']
I would like to call methods related to string in array
public function Render(){
foreach($array as $v)
// HERE CALL METHOD ($v) IN MY CLASS ex return: $this->header(), $this->navbar()
}
private function header(){
//EXEMPLE FESGRDGTFDTHTs
}
private function navbar(){
//EXEMPLE FESGRDGTFDTHTs
}
[ETC]
Ty for community
Just check if provided method exists (method_exists), if so then call it:
public function Render(){
foreach($array as $v)
if (method_exists($this, $v)) {
$this->$v();
}
}
}
Or you can use call_user_func function
public function Render() {
foreach($array as $v) {
if (method_exists($this, $v)) {
call_user_func(array($this, $v));
}
}
}
In PHP OOP, you can call a function with $this->functionName($etc)
. If you have basic knowledge with other programming language (espacially OOP), $this->
is like . (dot)
operator.