Consider the following class:
class output_Home {
public $app;
public $forums;
function __construct ($app) {
// main app class containing db, settings, etc
$this->app = $app;
// populate class property arrays for use in template
$this->setForums();
}
function setForums () {
/*
* select all forum data, dump into $this->forums array
* fields:
* id, name, slug, description, order,
* total_threads, total_posts,
* last_post_id, last_post_date, last_poster_id, last_poster_username, last_poster_avatar
*/
$sql = "select f.*,
p.id as last_post_id, p.date_created as last_post_date,
u.id as last_poster_id, u.username as last_poster_username, u.avatar as last_poster_avatar,
(select count(*) from `threads` where `id_forum`=f.id) as total_threads,
(select count(*) from `posts` where `id_forum`=f.id) as total_posts
from `forums` as f
left join `posts` as p on (p.id = (select `id` from `posts` where `id_forum`=f.id order by `date_created` desc limit 1))
left join `users` as u on (u.id = p.id_user)
order by f.order asc";
$stm = $this->app->db->prepare($sql);
$stm->execute();
$this->forums = $this->app->sanitizer->action('sanitize', $stm->fetchAll());
}
}
Here is my object_App
class:
class object_App {
public $db;
function __construct () {
// create database "db" connection
$this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
}
Here is how $this->app
is passed to the output_Home
class:
$app = new object_App();
$home = new output_Home($app);
The $app
property contains a property called db
, which is a PDO object. Notice the $stm
variable in particular here.
My problem is that I am getting warnings from PhpStorm claiming that methods such as $stm->execute()
are not found in the class. Well of course they aren't, because they are PDOStatements
.
I just want to know if there is a way I can get rid of the warnings properly, without using PHPDocs
above every single $stm
I make, because I will be making quite a few in several different class methods, and I don't want PHPDocs
everywhere in the code.
Any help would be appreciated.