0

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 \Ank\Config;
use \Ank\Repository;
use \Ank\Entity;
use \Ank\Db;

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 RpgForum\getInstance() in /app/src/RpgForum/Player.php:68 Stack trace: #0 /app/src/controller/connectionController.php(18): RpgForum\Player->setPlayer() #1 /app/src/controller/connectionController.php(25): RpgForum\Register->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).

ADyson
  • 57,178
  • 14
  • 51
  • 63
noaGepro
  • 11
  • 1
  • Warning: please don't store passwords using the obsolete, insecure md5 algorithm - that is another security risk. Learn about PHP's built-in, up-to-date, secure [password hashing and verification functions](https://www.php.net/manual/en/faq.passwords.php) instead. See also [How to use PHP's password_hash to hash and verify passwords](https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords) – ADyson Nov 03 '22 at 13:56
  • Sure, my tutor wanted me to use Postgresql functions but he said I could use the password_hash() function and I am a bit more used to work with this. Thank you for the help and your concern ! – noaGepro Nov 07 '22 at 16:20
  • `my tutor wanted me to use Postgresql functions`...well neither `crypt` or `gen_salt` are postgresql functions, they're PHP functions (as used in your code), so at this point you might as well use password_hash regardless of your tutor. – ADyson Nov 07 '22 at 16:24

2 Answers2

0

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/

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Hello, as you said, it makes sense to look what is in the Entity class but I couldn't find the getInstance() function. It is a public static function that I found in the Db class. But it's my internship tutor who extended Player class with Entity class and I can't have any anwser because he didn't reply to any of my messages since thursday so I have to continue alone for now... – noaGepro Oct 31 '22 at 07:05
  • @noaGepro If it's in your DB class and it's static, then you can call it like any other static function, eg. `TheClassName::getInstance();` . Example: https://www.geeksforgeeks.org/static-function-in-php/ – ADyson Nov 02 '22 at 12:23
  • That's it ! I could make it with this syntaxe and I now make use of the public static functions this way. Thanks for the tips ! – noaGepro Nov 03 '22 at 13:40
  • Thanks, I've adjusted the answer accordingly. Glad I could help. – ADyson Nov 03 '22 at 13:50
0

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
noaGepro
  • 11
  • 1
  • It's worth noting that making setPlayer static as well is not a pre-requisite for solving this - see my answer - although you may have other additional reasons for doing that. – ADyson Nov 03 '22 at 13:52