I would like to use $this->container->get in a custom class I've created. I've done my reading and found out that I should use ContainerInterface in the constructor, which I do, but I still get this error:
Error: Call to a member function get() on a non-object
Here is the code:
MyClass.php
namespace path\to\MyClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyClass {
private $container;
public $user_id;
public function __contruct(ContainerInterface $container) {
$this->container = $container;
$this->user_id = $user_id;
return $this;
}
/**
* @param string $data Some data
* @return array A response
*/
public function generatePDF($data)
{
// Create the folders if needed
$pdf_folder = __DIR__.'/../../../../web/pdf/'.$this->user_id.'/';
if(!file_exists($pdf_folder))
mkdir($pdf_folder, 0755, TRUE);
$file_id = "abc1";
// Set the file name
$file = $pdf_folder.$file_id.'.pdf';
// Remove the file if it exists to prevent errors
if(file_exists($file)) {
unlink($file);
}
// Generate the PDF
$this->container->get('knp_snappy.pdf')->generateFromHtml(
$this->renderView(
'StrimeGlobalBundle:PDF:invoice.html.twig',
$data
),
$file
);
}
}
Do you guys have any idea of what could be the problem?
Thanks for your help.