Thank you for reply
This is code I found on phpocean.com, it’s basically crud system, I am just trying to break it down, learn, and you are completely right about $this->dbh = $db;, that’s why I cant understand it…
As it stands in the code you posted it just sets $this->dbh to null again. So basically the code would be the same if you remove that line. It works, but it’s not doing anything useful.
Sounds like you should be careful about what you read on that site if they make these kinds of blatant mistakes.
If you type-hint the parameters, it’s much harder to instantiate with the wrong variables.
class Commons
{
protected $dbh;
public function __construct(PDO $db) {
$this->dbh = $db;
}
}
This way you can only pass PDO instances (before that you could pass anything, e.g. letters) and this now guarantees that $this->dbh will be a database connection.
I’ve also changed the property to be non-public, otherwise it could be changed at will making the property invalid again.