class Parent {
public $model;
public function test() {
return $this->model->orderBy();
}
}
//and then my controller
class foo extends Parent {
public function index() {
$this->model = new homeModel;
}
}
Well the issue would be if you call test then $this->model would be null.
Here's the same pseudo code replacing Parent which is a reserved keyword
class Bar {
public $model;
public function test() {
return $this->model->orderBy();
}
}
//and then my controller
class foo extends Bar {
public function index() {
$this->model = new homeModel;
}
}
Mocking the class and orderBy method
class homeModel {
function orderBy(){return 'order!';}
}
index() would need to be called before test() to set the model attribute to the new homeModel
$foo = new foo;
$foo->index();
echo $foo->test(); // order!
class Parent {
public $model;
public function __construct()
{
$this->model = new MyModel;
}
public function test()
{
return $this->model->orderBy();
}
}
class Foo extends Parent {
// You can override the model if you wish here
public function __construct()
{
parent::__construct();
$this->model = new OrderModel;
}
}
@kinshara I had assumed that. Sorry if I made that unclear with "Mocking the class and orderBy method". Regardless of framework if you had an empty php file and ran it the following it would illustrate the issue of the object needing to be init is what I was getting at.
<?php
class Bar {
public $model;
public function test() {
return $this->model->orderBy();
}
}
//and then my controller
class foo extends Bar {
public function index() {
$this->model = new homeModel;
}
}
class homeModel {
function orderBy(){return 'order!';}
}
$foo = new foo;
$foo->index();
echo $foo->test();