skip to Main Content

I’m beginning to learn the object-oriented programming in order to make a project : while I have some files that have been given to help me by my internship tutor, I can’t manage to work with it. So I struggle to make a basic insertion for registration.

Here is the model class Player :

<?php
declare(strict_types=1);

namespace RpgForum;

require_once(__DIR__ . '/../utils.php');

use AnkConfig;
use AnkRepository;
use AnkEntity;
use AnkDb;

class Player extends Entity{

  protected function setPlayer(string $username, string $mail, string $password){

    $db = getInstance();
    var_dump($db);
    
    $sql = $db->prepare('INSERT INTO player SET username = :username, mail = :mail, password = :password');
    $sql->bindValue(':username', $username);
    $sql->bindValue(':mail', $mail);
    $sql->bindValue(':password', crypt($password, gen_salt("md5")));
    $res = $sql->execute();
  }
}

And so here is the error :

Fatal error: Uncaught Error: Call to undefined function
RpgForumgetInstance() in /app/src/RpgForum/Player.php:68 Stack trace:
#0 /app/src/controller/connectionController.php(18): RpgForumPlayer->setPlayer() #1
/app/src/controller/connectionController.php(25):
RpgForumRegister->register() #2 {main} thrown in
/app/src/RpgForum/Player.php on line 68

Here is the thing : I have a class Player that uses a class Db and extends a class called Entity. getInstance() function is a public static function that I found in the Db class.

And so, I have an error telling that some of my attributes or methods are not defined, as if the link between classes could not be done…

So I tried to change what should be used or extended in term of classes. I tried to understand what my tutor gave me but it only disrupted some of my neurons. I took some online free courses to upgrade my knowledge and so I gave it a try with my new skills as I declared classes, new objects, some parameters and tried make a link with the database and view via the controller. But in the end I can’t see in the database the new player, showing me that something failed (see the error thrown).

2

Answers


  1. Chosen as BEST ANSWER

    I could resolve my problem this way:

    Insted of protected, I defined my method setPlayer() as a public static function and by extension I called my getInstance() method from the Db class by doing this :

    $db = Db::getInstance();
    

    And so I could walk through new errors that I could resolve and now everything is working so far.


  2. According to your description, getInstance is a static function in your Db class.

    Therefore you need to use code in the form Db::getInstance(); to call it, so the code knows to look inside that class to find the function.

    Simplified demo: https://3v4l.org/XrBC3

    Demo code copied here, for easy reference:

    class Db
    {
        public static function getInstance()
        {
            return "Hi, I'm a test";
        }
        
    }
    
    class Entity
    {  
    }
    
    class Player extends Entity
    {
        public function setPlayer()
        {
            return Db::getInstance();
        }
    }
    
    $x = new Player();
    echo $x->setPlayer();
    

    Reference: https://www.geeksforgeeks.org/static-function-in-php/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search