【发布时间】:2015-03-10 21:21:12
【问题描述】:
class/dbConnect.php 页面
class dbConnect {
/*
* initiate mysql database host,username,password,database name
*/
private $host;
private $dbName;
private $uname;
private $upass;
private $con;
public function __construct($host, $database, $userName, $password) {
$this->host = $host;
$this->dbName = $database;
$this->uname = $userName;
$this->upass = $password;
$this->connectDB();
}
public function connectDB() {
/*
* @var $dsn mean data source name for pdo connection
*/
$dsn = "mysql:host=" . $this->host . ";dbName=" . $this->dbName;
try {
$this->con = new PDO($dsn, $this->uname, $this->upass);
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
class/sampleView.php
/*
* include dbConnect class for this SampleView Class Page
*/
require_once './dbConnect.php';
class sampleViews extends dbConnect{
function viewUsers(){
/*
* dbConncect Class Connection variable access
*/
$connection = $this->con;
}
}
这样对吗???我无法使用带有 sampleView 类的 $this-> 方法访问 dbConnect 类 $con 变量
并且可以通过这种方式完成不同的位置类扩展包括页面..帮助Plezzzz
【问题讨论】:
-
将属性从
private更改为protected