T
T
Twelfth Doctor2017-03-28 15:57:57
phpstorm
Twelfth Doctor, 2017-03-28 15:57:57

Why doesn't PHPStorm see the variable from the class constructor?

Hello! There is such a class:

class Site
{
//конструктор класса
    public function __construct($login, $id = 0, $mainname = 0, $charset = 0, $email = 0, $index = 0)
    {
        $this->id = $id;
        $this->userid = $login;
        $this->mainname = $mainname;
        $this->charset = $charset;
        $this->email = $email;
        $this->index = $index;
        //connect to database
        $dbhost = "127.0.0.1";
        $dbuser = "***";
        $dbpass = "***";
        $dbname = "****";
        $connect = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass , [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    }
//метод класса
  public function getsiteinfo($login, $id)
    {

        $getquery = $connect->prepare("SELECT * FROM `***` WHERE `***` = :login AND `unid` = :unid");
        $getquery->execute(array(":login" => $login,  ":unid" => $id));
        $info = $getquery->fetch();
        return $info;
    }
}

PHPStorm underlines $connect variable in getsiteinfo()red, error: Undefined variable 'connect'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2017-03-28
@verdex

Because variables are not passed between methods.
If you want to reuse something, you need to save it in the object parameters:
$this->connection = ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question