thecodeholic / php-mvc-framework

PHP MVC Framework
https://www.youtube.com/playlist?list=PLLQuc_7jk__Uk_QnJMPndbdKECcTEwTA1
473 stars 178 forks source link

Fatal error: primaryKey() #21

Open tarzinio opened 2 years ago

tarzinio commented 2 years ago

Fatal error: Uncaught Error: Non-static method thecodeholic\phpmvc\db\DbModel::primaryKey() cannot be called statically in C:\wamp64\www\link\vendor\thecodeholic\php-mvc-core\Application.php on line 54

obraz

thecodeholic commented 2 years ago

Thanks for submitting the issue. I will do my best to fix that ASAP. If you want you can make a PR I would merge it. The changes should be made here. https://github.com/thecodeholic/tc-php-mvc-core/blob/master/db/DbModel.php#L23

HoosierExperienceDesigner commented 2 years ago

I'm experiencing a similar issue but mine stems from the Application.php file. Everything was working perfect until about the 4:25:00 point in the lesson. I used var_dump at the bottom of Application constructor to target the specific bug, and for me it is occurring in this line:

$primaryKey = $this->userClass::primaryKey();

Fatal error: Uncaught Error: Non-static method app\models\User::primaryKey() cannot be called statically in D:\xampp\htdocs\I211_Project\core\Application.php:45 Stack trace: #0 D:\xampp\htdocs\I211_Project\public\index.php(24): app\core\Application->__construct('D:\xampp\htdocs...', Array) #1 {main} thrown in D:\xampp\htdocs\I211_Project\core\Application.php on line 45

var_dump indicates it is returning the id, and the PHP cookie does exist.

Screenshot 2022-04-01 163225

RobertoVasquez01 commented 2 years ago

All these problems with static calls are due to the php version. It has to be higher than 7.4. You can temporarily modify the function so that it is not static, but later in the lesson you will experience a similar problem. That's why it's better to update the php version

tarzinio commented 2 years ago

I am using version 8.1

pon., 4 kwi 2022 o 06:56 RobertoVasquez01 @.***> napisał(a):

All these problems with static calls are due to the php version. It has to be higher than 7.4. You can temporarily modify the function so that it is not static, but later in the lesson you will experience a similar problem. That's why it's better to update the php version

— Reply to this email directly, view it on GitHub https://github.com/thecodeholic/php-mvc-framework/issues/21#issuecomment-1087115363, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG7LDYVQUKWUMGYJDNPBEVDVDJZALANCNFSM5R2XYSNA . You are receiving this because you authored the thread.Message ID: @.***>

HoosierExperienceDesigner commented 2 years ago

I also applied PHP settings to 8.0.1, and to be sure ran 'php -v' in the terminal with the following result:

PS D:\xampp\htdocs\I211_Project> php -v PHP 8.0.1 (cli) (built: Jan 5 2021 23:43:39) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.1, Copyright (c) Zend Technologies

I still get the same fatal error Non-static method app\models\User::primaryKey() cannot be called statically

RobertoVasquez01 commented 2 years ago

Yes, I was wrong in the first part of my comment. It was because I'm using php version 7.3 and throughout the video tutorial I had to make a few small changes to take advantage of this great input from @thecodeholic, and I was quick to say that the error is based on the php version. You have to MOMENTARY change the declaration: "abstract public static function primaryKey(): string;" from the DbModel class to this: "public function primaryKey(): string;" and call it like this: DbModel->primaryKey(); I repeat: This is momentary until the video tutorial continues, which then will have to return the abstract declaration... and it will no longer give you a problem.

HoosierExperienceDesigner commented 2 years ago

Thank you for the information! Will give this a shot and provide feedback on whether or not it was successful.

HoosierExperienceDesigner commented 2 years ago

I eventually started over and rebuilt the app from the beginning and got it to work.

RobertoVasquez01 commented 2 years ago

Perfect! Thanks to @thecodeholic

rlhopkins commented 2 years ago

Howdy, love this mvc-framework! Been trying to fix error around the end of Login section. I still keep getting error for "findOne". I've read through these comments, get a different error, so I undid the changes. This is the current error: Fatal error: Uncaught Error: Non-static method app\core\DbModel::findOne() cannot be called statically in /var/www/html/familyweb/models/LoginForm.php:45 Stack trace: #0 /var/www/html/familyweb/controllers/AuthController.php(32): app\models\LoginForm->login() #1 [internal function]: app\controllers\AuthController->login() #2 /var/www/html/familyweb/core/Router.php(57): call_user_func() #3 /var/www/html/familyweb/core/Application.php(60): app\core\Router->resolve() #4 /var/www/html/familyweb/public/index.php(39): app\core\Application->run() #5 {main} thrown in /var/www/html/familyweb/models/LoginForm.php on line 45 login_error login_error

RobertoVasquez01 commented 2 years ago

Declare the findOne function as static in the DbModel class. public static function findOne($where){ }