It's been a little while since I've worked with constructors in CI. I've looked at the userguide for CI4, and constructors seem to be a bit different to CI3. I've copied the code and trialled it, but I get an error message: Cannot call constructor
.
public function __construct(...$params)
{
parent::__construct(...$params);
$model = new ShopModel();
$findAll = [
'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
];
}
From here, I've searched online and saw a similar thread which advised removing the parent::__construct(...$params);
line altogether. When I do this, the page loads - but the $findAll array is NULL when I try to call it in the Controller function I need it for:
public function brand_name($brand_name_slug)
{
$model = new ShopModel();
var_dump($findAll['shop']);
$data = [
'shop' => $findAll['shop'],
];
echo view('templates/header', $data);
echo view('shop/view', $data);
echo view('templates/footer', $data);
}
Advice or pointers greatly appreciated!
$findall
should be a class variable (declared inside the class but outside all the methods) and accessed/modified with the this keyword like so:
Class Your_class_name{
private $findAll; //This can be accessed by all class methods
public function __construct()
{
parent::__construct();
$model = new ShopModel(); //Consider using CI's way of initialising models
$this->findAll = [
'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
]; //use the $this keyword to access class variables
}
public function brand_name($brand_name_slug)
{
...
$data = [
'shop' => $this->findAll['shop'], //use this keyword
];
....
}