PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Extending CakePHP's AppController with functions from other classes

 January 09, 2022     cakephp, cakephp-3.0     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing