Skip to content

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

Closed
araragi-koyomi-sama opened this issue Feb 25, 2021 · 16 comments · Fixed by tobidosumu/tobiscore#2

Comments

@araragi-koyomi-sama
Copy link

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)

@araragi-koyomi-sama
Copy link
Author

also, if someone find a solution to this kind of problems whitout making the functions statics I'm looking forward to heard about it

@onabright
Copy link

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.

@mahmoodhussam
Copy link

mahmoodhussam commented Jul 1, 2021

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

@karam-mustafa
Copy link

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

@AlanSMat
Copy link

AlanSMat commented Nov 8, 2021

Thank you @karam-mustafa, that worked for me.

@eisa97
Copy link

eisa97 commented Dec 23, 2021

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

hi
thank you; but how can i use $app->router->get('/about', [AboutController::class, 'index']) exactly, because this is used on a tutorial movie. but do not work for me.

@none-08
Copy link

none-08 commented Dec 25, 2021

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

Thanks a lot, i was trying to solve this for quite a while and i was about to give up

@Mouhamed120
Copy link

[::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
Stack trace:
#0 C:\xampp\htdocs\learn_php\01_theholicod\15-autoloading\better\Router.php(35): call_user_func()
#1 C:\xampp\htdocs\learn_php\01_theholicod\15-autoloading\better\public\index.php(19): app\Router->resolve()
#2 {main}
thrown in C:\xampp\htdocs\learn_php\01_theholicod\15-autoloading\better\Router.php on line 35
Help

@Mouhamed120
Copy link

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

@MohammadRezaeeGithub
Copy link

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:
return call_user_func([new $callback[0],$callback[1]]);

@imManish
Copy link

imManish commented Jul 30, 2022

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;
}

@frankndungu
Copy link

This worked for me.

$app->router->get('/about', [AboutController::class, 'index']); // from this

$app->router->get('/about', [new AboutController(), 'index']); // to this

@MajedHasan
Copy link

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?

@frankndungu
Copy link

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

@frankndungu
Copy link

frankndungu commented Dec 1, 2022 via email

@kapasulediesel5
Copy link

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: return call_user_func([new $callback[0],$callback[1]]);

could you please paste full code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.