Witam, dostaję taki błąd:
Uncaught Error: Using $this when not in object context
Próbuję w pliku Twig wywołać funkcję w ten sposób:
{{ first_name|add_worker(name, date_birthday, brand) }}
Funkcję przekazałem tak jak jest to pokazane w dokumentacji:
$filter = new Twig_SimpleFilter('add_worker', array('Worker', 'add_worker'));
$twig->addFilter($filter);
Moja classa wygląda tak:
class Worker{
private $first_name;
private $name;
private $date_birthday;
private $brand;
private $db;
public function __construct($DB_con){
$this->db = $DB_con;
}
public function add_worker(string $first_name, string $name, string $date_birthday, string $brand){
$this->first_name = $first_name;
$this->name = $name;
$this->date_birthday = $date_birthday;
$this->brand = $brand;
try{
$update = $this->db->prepare("INSERT INTO workers (first_name, name, date_birthday, brand) VALUES ('$this->first_name', '$this->name', '$this->date_birthday', '$this->brand')");
$update->execute();
return true;
}
catch (PDOException $e){
echo $e->getMessage();
return false;
}
}
[cd...]
}
Problem jest parametrem $this, bo jak usunę wszystkie $this i moja funkcja będzie miała np. wyświetlić przekazane parametry przez echo, to działa.