1

I am having a hard time trying to figure the error in the code. There some similar issues on SO, but they did not help that much with my specific problem. I also googled every possible phrases regarding the error, yet still no joy. In ProductCategoryController.php I have:

namespace App\controllers\admin;


use App\classes\CSRFToken;
use App\classes\Request;
use App\classes\ValidateRequest;
use App\models\Category;

class ProductCategoryController
{
    public $table_name = 'categories';
    public $categories;
    public $links;

    public function __construct()
    {
        $obj = new Category();
        $total = Category::all()->count(); // total number of rows
        list($this->categories, $this->links) = paginate(3, $total, $this->table_name, $obj);
    }

    public function show() {
        return view('admin/products/categories',
            [
                'categories' => $this->categories,
                'links' => $this->links
            ]);
    }

}

I get error

using $this when not in object context

on line 27 where I assign 'categories' => $this->categories and 'links' => $this->links

When I try setting 'categories' and 'links' to an empty array, everything work fine as expected.

enter image description here

In the RouteDispatcher.php I have: enter image description here

Perhaps I may be missing something very obvious, any support with my problem is well appreciated.

5
  • 2
    How are you calling ProductCategoryController's show method? Commented Feb 17, 2018 at 17:06
  • 1
    Probably statically ProductCategoryController::show() Commented Feb 17, 2018 at 17:08
  • 1
    Stack in image suggests RouteDispatcher uses call_user_func_array, to call the controller's show method. Would be good to see more of this code. Commented Feb 17, 2018 at 17:14
  • @Progrock I will attach now. Commented Feb 17, 2018 at 17:34
  • 1
    Please inline code, rather than images. Commented Feb 17, 2018 at 17:36

1 Answer 1

3

In your dispatcher, you are calling your controller's method statically.

In your code you test if your method is callable on a new instance. And then don't go on to reuse that newly created instance when calling. Instead you use the class and method name in call_user_func_array - and therefore call it statically, which results in your error.

Try and change your code to something more like this:

$controller = new $this->controller;
$method     = $this->method;

if(is_callable(array($controller, $method)))
    call_user_func_array(array($controller, $method), $params);

Or move the new:

if(is_callable(array($this->controller, $this->method)))
    call_user_func_array(
        array(new $this->controller, $this->method),
        $params
    );
Sign up to request clarification or add additional context in comments.

1 Comment

You are a king @Progrock. Thank you so so very much. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.