データベース クラスとデータベース アクセス オブジェクトを作成したので、どうにかして値オブジェクト パターンを実装できます。
データベース クラスの一部とその機能を次に示します。
class Database {
protected $conn = null;
private $stmt;
// create a connection
public function __construct($dsn, $username, $passwd) {
try {
// mysql and pdo
$this->conn = new PDO($dsn, $username, $passwd);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->get_error($e);
}
}
}
このdbクラスの一部は、私が作成した関数ですnum_rows
public function num_rows($query) {
$this->stmt = $this->conn->prepare($query);
if ($this->stmt) {
$this->stmt->execute();
return $this->stmt->rowCount();
}
}
また、実際に目に見えることを行うこの DAO クラスもあります。
class Dao {
protected $db = null;
public function __construct() {
$dbh = new Database('mysql:host=localhost;dbname=doorche', 'root', '');
$this->db = $dbh->getConnection();
}
//put your code here
}
たとえば、別のクラスでDAOクラスを拡張するとloginDao
、* num_rows *に何らかの方法でアクセスできません
なぜそれが起こっているのですか?