-
Notifications
You must be signed in to change notification settings - Fork 184
Solution to " Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, non-static method app\controllers\SiteController::contact() cannot be called statically" error during the implementation of the controller #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
also, if someone find a solution to this kind of problems whitout making the functions statics I'm looking forward to heard about it |
Hi guys, I am also using PHP 8.x and I had to declare my functions as static to resolve the error: Uncaught TypeError: call_user_func(). Hopefully I will figure out how to do it without using static methods. |
I think to solve this problem , when you used call_user_fun without made object from the class, you could use only static function but when you used object from this class you could use pure functions |
you can solve problem by make new instance from this class, like this
|
Thank you @karam-mustafa, that worked for me. |
hi |
Thanks a lot, i was trying to solve this for quite a while and i was about to give up |
[::1]:62171 [500]: GET /products/create - Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, non-static method app\controllers\ProductController::create() cannot be called statically in C:\xampp\htdocs\learn_php\01_theholicod\15-autoloading\better\Router.php:35 |
I think the problem is solved but I have not tested$app->router->get('/about', [AboutController::class, 'index']); // from this $app->router->get('/about', [new AboutController(), 'index']); // to this |
I use php 8.* as well and I had the same problem. because I liked the way we write our route , I changed my resolve method in Router class. I called the function like this: |
you can solve this by checking if your callback is_array or string return call_user_func($this->isCheck($callback));
/**
* @param $callback
* @return mixed
*/
private function isCheck($callback)
{
if(is_array($callback))
$callback[0] = new $callback[0];
return $callback;
} |
This worked for me. $app->router->get('/about', [AboutController::class, 'index']); // from this $app->router->get('/about', [new AboutController(), 'index']); // to this |
Thank you for sharing this with us :) |
The error has to do with your php version, when I downgraded to 7.4 the first option worked well but if you are on php 8^ it won't work |
The error has to do with your php version, when I downgraded to 7.4 the
first option worked well but if you are on php 8^ it won't work
…On Thu, 1 Dec 2022 at 18:18, Mehedi Hasan Majed ***@***.***> wrote:
you can solve problem by make new instance from this class, like this
$app->router->get('/about', [AboutController::class, 'index']); // from this
$app->router->get('/about', [new AboutController(), 'index']); // to this
Thank you for sharing this with us :)
But can you please tell me why this issue is happening?
—
Reply to this email directly, view it on GitHub
<#3 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARERJNPGAM5DSVHCTNJ3WRDWLC6UNANCNFSM4YHJXYDA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
could you please paste full code? |
hello, i'm using php 8.x, i solved this problem making static every function in SiteController (where i had to use SiteController), based on the documentation in the version 8.x the operator :: is used for static functions, i also had to use use app\core\Application in the SiteController to include Application in SiteController. I write down this here just in the case other one had the same problem. (sorry for my english, I'm not a native speaker)
The text was updated successfully, but these errors were encountered: