我遇到两个班的问题,我不是php,pdo和对象的专家。
这是问题,我有一个类数据库
<?php
/**
* Database class
*/
class database
{
protected $conection;
function __construct($conectionData, $user, $password){
$this->conection = new PDO($conectionData, $user, $password);
$this->conection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->conection;
}
}
?>
和扩展的类
<?php
/**
*
*/
class micros extends database
{
public function getAll(){
$micros = array();
$sql = 'SELECT * FROM micros;';
$sth = $this->conection->prepare($sql);
if($sth != false) {
$sth->setFetchMode(PDO::FETCH_CLASS, 'Micro');
$sth->execute();
while($micro = $sth->fetch()) {
$micros[] = $micro;
}
}
return $micros;
}?>
我可以通过使用数据库类构造函数
来调用扩展类$db = new database("mysql:host=localhost;dbname=micros_admin","root","");
$micros = new micros("mysql:host=localhost;dbname=micros_admin","root","");
$all = $micros->getAll();
像这样,我想只调用一次数据库类,然后能够调用任何其他类而无需再次编写数据库类的参数,任何帮助都表示赞赏。