PHP concatenate string + variable with obj-method

0

I want to call a method automatically if a given situation occurs so I am trying to mount the method name.

The methods I'm going to do this are gets and seters:

$obj->get  +   $restanteDoNomeDoMetodo + ()

When trying to perform this join, this error is occurring, the code interpreter is identifying get as variable, and is it part of the name of a method that exists in a class some idea how to solve it? >

I tried some variations to concatenate as terms between '' and {} all of them result in error

$stmt->bindValue(":{$this->columns[$i]}",
                 $this->entity->{'get'.ucfirst($this->columns[$i]).'()'});

Someone could help me in this matter already thanks.

Erro: Notice: Undefined property: Cliente::$getNome() in
      C:\Users\Vinicius\Desktop\pdo\ServiceDb.php on line 67
1 
    
asked by anonymous 12.03.2018 / 18:04

1 answer

0

You can assemble the method name using the concept of various functions

<?php
//classe para de teste com get e set
class teste{
  private $nome = "";
  public function getNome(){
   return $this->nome;
    }
  public function setNome($nome){
   $this->nome = $nome ;
   }
}

//criando um objeto para utilizar
$teste = new teste();

//chamando o primeiro método através de um funcção variável
$metodo = "setNome";

//fazendo o teste para verificar se realmente existe...
if(is_callable(array($teste, $metodo))){
  $teste->$metodo("NOME DO FULANO");
}else{
  // trate aqui o erro caso nao exista...
}

// trocando o método
$metodo = "getNome";

if(is_callable(array($teste, $metodo))){
  echo $teste->$metodo();
}else{
  // trate aqui o erro caso nao exista...
}

the result will be

NOME DO FULANO
    
12.03.2018 / 18:22