我有一个问题,了解类扩展如何工作..
我正在尝试扩展一个类来分割不同文件中的函数,使其更有条理.
但我有问题访问主类的变量和函数到子类.
那是我的意思:
父类:它是uFlex类v 0.88
我不写所有,因为它很长..
class uFlex {
//Constants
const version = 0.88;
const salt = "";
//End of constants\\\\
/**
* PDO / database credentials
*/
var $db = array(
"host" => '',
"user" => '',
"pass" => '',
"name" => '', //Database name
"dsn" => '' //Alterntive PDO DSN string
);
function connect(){
if(is_object($this->db)) return true;
/* Connect to an ODBC database using driver invocation */
$user = $this->db['user'];
$pass = $this->db['pass'];
$host = $this->db['host'];
$name = $this->db['name'];
$dsn = $this->db['dsn'];
if(!$dsn){
$dsn = "mysql:dbname={$name};host={$host}";
}
$this->report("Connecting to database...");
try{
$this->db = new PDO($dsn, $user, $pass);
$this->report("Connected to database.");
}catch(PDOException $e){
$this->error("Failed to connect to database, [SQLSTATE] " . $e->getCode());
}
if(is_object($this->db)) return true;
return false;
}
}
然后:
class admin extends uFlex {
function adm_getUsers(){
if(!$this->connect()) return false;
$sql= "SELECT * from users LIMIT 30";
$st = $this->db->prepare($sql);
$out = $st->execute();
$row = $st->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
function adm_getSingleUser($id){
if(!$this->connect()) return false;
if(is_numeric($id)){
$sql= "SELECT * from users WHERE id = '$id'";
}else{
$sql= "SELECT * from users WHERE username = '$id'";
}
$st = $this->db->prepare($sql);
$out = $st->execute();
$row = $st->fetch(PDO::FETCH_ASSOC);
return $row;
}
}
?>
我在每个页面中包含的配置文件中对它们进行了实际操作:
$user = new uFlex(false);
$admin = new admin();
但是当使用$admin-> adm_getUsers(); $row数组它是空的.
在尝试在两个类之间拆分函数之前,我在主类上使用了相同的函数,并且正在工作.
这是我第一次尝试扩展一个类..我在谷歌搜索,并在这里提出了一些问题,但这对我来说太复杂了,因为我还在学习PHP.