1
I have a simple class with 2 methods, here is the code:
namespace app\Services;
use App\Repositories\LogRepository;
class LogService
{
protected $repository;
public function __construct(LogRepository $repository){
$this->repository=$repository;
}
public function cadastrar($dados){
return 'ola2';
}
public function deletar($id=0)
{
return 'ola';
}
}
And in another class I call this class:
namespace app\Classes;
use App\Repositories\LogRepository;
use app\Services\LogService;
class Log
{
private static $repository;
private static $service;
public function __construct(LogRepository $repository, LogService $service)
{
self::$repository=$repository;
self::$service=$service;
}
static function Gravar($tabela, $evento, $sql=null, $valoresAntigos, $valoresNovos){
self::$service->deletar(1);
return true;
}
}
When I run me the following error:
Fatalerrorexception in Log.php line 29: Call to a Member Function deletar() on null
What am I forgetting to do or doing wrong? Thank you
I think: you create an instance of $service in your Log class constructor. But the Save function is static. Therefore, the constructor is not executed. It’s just a kick, I’ve never used PHP.
– RSinohara
Can you make this right? Because I need this static class.
– Joao Nivaldo
To test try creating the instance in the write function itself.
– RSinohara