fetch_object - Method not found warnings

Hi,

I get some Method not found warnings on some standard mysqli methods that I use to fech objects or free results.
The Code works fine but I dont know how I resolve the warning messages.

CODE example:
public function getUserById($id){

      $mydb = Mysql::getInstance();
 

      $getUser = $mydb->query("SELECT * FROM data WHERE id='".$id."' LIMIT 1");

     $gotUser = $getUser->fetch_object('User');
     $getUser->free_result();

 
      return $gotUser;

 }

WARNINGS are for example:
Method 'fetch_object' not found in class
Referenced method is not        found in subject class.
-
Method 'free_result' not found in class
Referenced method is not        found in subject class.

Thanks!

8 comments
Comment actions Permalink

Hi there,

Please provide code for your Mysql class.

Most likely it does not have proper PHPDoc comments for those methods, which prevents IDE from properly identifying what method returns what etc.

0
Comment actions Permalink

As suspected it has no PHPDoc comments at all and IDE is unable to figure out what exactly each function/method returns from such code only.

You need to add PHPDoc comments for each method to help IDE with proper return types.

Since you used class from the link as a base for your own class, I cannot tell you what to put where without seeing actual code, since your class is not the same as the one in aforementioned link.

0
Comment actions Permalink

I use the exact same class as shown from the link.
The mysql class extends from mysqli.
fetch_object and free_result are methods not written by me. They come with the extended mysqli class.

How can I add PHPdoc for classes that are basic part of mysqli?

0
Comment actions Permalink

I use the exact same class as shown from the link.

If that is the case (exact same class)...
1) I do not see getInstance method anywhere in that database class there
2) In that class, both query() and execute() methods return nothing (void .. or null in PHP terms), therefore $getUser = $mydb->query('...'); makes no sense as $getUser will alvays be null and $gotUser = $getUser->fetch_object('User'); will always fail with PHP error.

Still "exact same class" .. or you have made some modifications to the original database class ?

fetch_object and free_result are methods not written by me.

Yes, but $mydb->query() is written by you. Which means that you can add correct PHPDoc (in particular, @return tag with correct return type) for that method easily.


In any case: if you do not want (or cannot) add PHPDoc into your Mysql class, you can make this workaround (which is not that good as it only affects this particular location) -- add correct typehint via PHPDoc in actual code:

/** @var mysqli_result $getUser */
$getUser = $mydb->query("SELECT * FROM data WHERE id='" . $id . "' LIMIT 1");

Now "fetch_object" and "free_result" methods are not longer "not found".

0
Comment actions Permalink

thanks ;-)
I think I know what happend. Clicking the link it takes a while until it jumps to Post #4. Its the class in this post, not in the 1st one.

This is the class of post #4 from the link:

<?php
/**
*  Database class to implement singleton pattern on top of mysqli
*/
class Database extends mysqli
{
   /**
   *  @var object Singleton instance
   */
   private static $instance = null;
    
   // DB connection parameters:
   private $dbHost = 'localhost';
   private $dbUser = 'xxxxxx';
   private $dbPwd  = 'yyyyyy';
   private $dbName = 'zzzzzzzz';
    
   /**
   *  Constructor
   *  @return void
   */
   private function __construct()
   {
      @parent::__construct(
         $this->dbHost,
         $this->dbUser,
         $this->dbPwd,
         $this->dbName
      );
      if(mysqli_connect_errno())
      {
         throw new Exception(
            mysqli_connect_error(),  
            mysqli_connect_errno()
         );
      }
   }
    
   /**
   *  Do the singleton thing
   *  @return object Database
   */
   public function getInstance() {
      if(self::$instance === null)
      {
         $c = __CLASS__;
         self::$instance = new $c;  
      }
      return self::$instance;
   }

   public function __clone()
   {
      throw new Exception("Cannot clone ".__CLASS__." class");
   }
}



But anyway, I added the PHPdoc you mentioned and it works ;-)
Thanks. Now I know I need to 1) learn PHPdoc and 2) add more comments :D

0
Comment actions Permalink

Fixing few errors and now it works just fine:

/**
*  Database class to implement singleton pattern on top of mysqli
*/
class Mysql extends mysqli
{
   /**
   *  @var self Singleton instance
   */
   private static $instance = null;
   
   // DB connection parameters:
   private $dbHost = 'localhost';
   private $dbUser = 'xxxxxx';
   private $dbPwd  = 'yyyyyy';
   private $dbName = 'zzzzzzzz';
   
   /**
   *  Constructor
   */
   private function __construct()
   {
      @parent::__construct(
         $this->dbHost,
         $this->dbUser,
         $this->dbPwd,
         $this->dbName
      );
      if(mysqli_connect_errno())
      {
         throw new Exception(
            mysqli_connect_error(),
            mysqli_connect_errno()
         );
      }
   }
   
   /**
   *  Do the singleton thing
   *  @return self
   */
   public static function getInstance() {
      if(self::$instance === null)
      {
         $c = __CLASS__;
         self::$instance = new $c;
      }
      return self::$instance;
   }


   public function __clone()
   {
      throw new Exception("Cannot clone ".__CLASS__." class");
   }
}

0
Comment actions Permalink

yeah, thats nice. Thank you for your help!

0

Please sign in to leave a comment.