Pdo autocompletion

Hi,
I am setting op a db class as an excercise to learn php a bit better.
In the code below in the method querydb I get a warning from phpstorm that it does not now
about the $this->dblink and also the method $this-dblink->query is unknown.
What am I doing wrong ?

 
class dbclass
{
    var $dbhost;
    var $dbname;
    var $dbuser;
    var $dbpass;
    var $dbschema;
    var $dblink;

    function __construct($h, $n, $u, $p, $s)
    {
        $this->dbhost = $h;
        $this->dbname = $n;
        $this->dbuser = $u;
        $this->dbpass = $p;
        $this->dbschema = $s;
    }


    function opendb()
    {
        try {
            /*** setup the connection ***/
            $this->dblink = new PDO('pgsql:dbname=' . $this->dbname . ';host=' . $this->dbhost, $this->dbuser, $this->dbpass);
            /*** set the error reporting attribute ***/
            $this->dblink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            /*** test the new connection ***/
            $sql = 'SELECT version()';
            $resultset = $this->dblink->query($sql);
            if ($resultset->rowCount() <= 0) {
                throw new Exception('<p>Unable to perform test query</p>');
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        } catch (Exception $e) {
            echo $e->getMessage();
        }

    }

    function querydb($sql)
    {
        try {
            return $this->dblink->query($sql);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

Screenshot from 2015-02-11 15:30:51.png



Attachment(s):
question.png
4 comments
Comment actions Permalink

Hi there,

IDE does not know what of type the $dblink field is.

It tracks it fine within the same method only (no warnings in opendb() ) .. but knows nothing about it in querydb().

Solution -- use PHPDoc comment to provide typehint (where PDO is a type/class name):

/** @var PDO Optional comment here */
var $dblink;


P.S.
Using var keywords to declare class variables is PHP 4 style -- in modern PHP (v5.x) you usually do not use it -- just visibility modifier, e.g. protected $dblink;

Useful links for PHPDoc:


Other links (PhpStorm):

0
Comment actions Permalink

Thanks so much !!

This did the trick.
Is php 5.5 also php 4 style ? so to use the modern way I would have to upgrade from 5.5 to 5.6 ?

Regards and thanks for helping out !!

Hans

0
Comment actions Permalink

It meant to be 5.x (or just 5) instead of 5.6.

5.6 is just the latest version.

0
Comment actions Permalink

Hi Andriy,

First of all thanks for your patience....

I got it!

0

Please sign in to leave a comment.