Issue
I wan't to extend the default AppController class by function from another class from the 'library' folder inside the application root.
On this moment I can reach the class and his function by adding a @property
declaration above my class definition like below. But when I run the application it's returning a Call to a member function showTest() on boolean
exception. Is this because I've not declared a namespace or something in that way?
// Default class inside 'root/src/Controller/'
/**
* Class AppController
*
* @property testControl $testControl
*
* @package App\Controller
*/
class AppController extends Controller
{
public function initialize() {
parent::initialize();
}
public function beforeFilter(Event $event) : void
{
$this->testControl->showTest();
}
}
// The class inside folder 'root/library/'
class testControl
{
public function showTest() {
die("test");
}
}
Solution
You need to create a new instance of the testControl
object before calling the method:-
public function beforeFilter(Event $event) : void
{
$testControl = new testControl;
$testControl->showTest();
}
The PHP error you are seeing is because you haven't initiated the object and $this->testControl
hasn't been defined.
You will also need to make sure you tell PHP where to find the testControl
class by adding a use
statement at the top of your file or referencing the namespace when initiating the object.
Answered By - drmonkeyninja
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.