我有这个:
class Database{
private $name = '';
private $user = '';
private $password = '';
private $host = 'localhost';
private $prefix = '';
private $connection_handle = null;
private function Connect(){
$this->connection_handle = mysql_connect($this->host, $this->user, $this->password);
if( !$this->connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( $this->name, $this->connection_handle );
}
}
private function Close(){
mysql_close( $this->connection_handle );
}
public static function Query( $query ){
$this->Connect();
$result = mysql_query( $query, $this->connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->result = $result;
$DatabaseQuery->mysql_num_rows = mysql_num_rows($result);
return $DatabaseQuery;
}
$this->Close();
}
}
我已将 Query 设为静态,因为它将被称为“Database::Query” 其他函数不需要由任何其他类访问,因此我将它们设为私有...
从 Query 函数访问 Connect 函数时出现此错误。
“不在对象上下文中使用 $this”
考虑到私有使这些变量对此类私有但对其中的所有方法都可用,我在获取函数范围时遇到了问题......
我可以做 self::Connect() 但我对它的理解不足以保证使用它......
你能解释一下 self:: 和 %this-> 之间的区别吗?
我也无法访问我的私有变量,并且它们真的不需要在此类之外访问,因此我再次将它们设为私有......但是 $this->connection_handle 没有该变量的范围。
我真的需要将它们全部公开吗?必须有一种方法使它们仅可用于此类及其扩展吗?
- - - - - - - - - - - 编辑:///
我最终将所有变量设为私有静态。这是一种可接受的方式吗?
- - - - - - - - - - - 编辑:///
我现在得到了这个代码:
class Database{
private static $name = '';
private static $user = '';
private static $password = '';
private static $host = 'localhost';
private static $prefix = '';
private static $connection_handle = null;
private static function Connect(){
self::$connection_handle = mysql_connect(self::$host, self::$user, self::$password);
if( !self::$connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( self::$name, self::$connection_handle );
}
}
private static function Close(){
mysql_close( self::$connection_handle );
}
public static function Query( $query ){
self::Connect();
$result = mysql_query( $query, self::$connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->$result = $result;
$DatabaseQuery->$mysql_num_rows = mysql_num_rows($result);
return &$DatabaseQuery;
}
self::Close();
}
}
但是返回指针引用不起作用?我实际上对 PHP 指针不是很了解,有人对我在这里做错了什么有所了解吗?
这是因为我在函数中声明了 DatabaseQuery 类的新实例,不是吗。帮助:)
编辑::///////
我已经完成了我的课程,它看起来像这样:
class DatabaseQuery{
public $result;
public $mysql_num_rows;
}
class Database{
private static $name = '';
private static $user = '';
private static $password = '';
private static $host = 'localhost';
private static $prefix = '';
private static $connection_handle = null;
protected function Connect(){
self::$connection_handle = mysql_connect(self::$host, self::$user, self::$password);
if( !self::$connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( self::$name, self::$connection_handle );
}
}
protected function Close(){
mysql_close( self::$connection_handle );
}
public static function FetchQueries( &$queries ){
$db_query = array();
self::Connect();
foreach( $queries as $key => $query ){
$result = mysql_query( $query, self::$connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->result = $result;
$DatabaseQuery->mysql_num_rows = mysql_num_rows($result);
$db_query[ $key ] = $DatabaseQuery;
}
}
self::Close();
return $db_query;
}
}
现在您可以以模型视图控件 (MVC) 的方式调用它,因为您在顶部(或在单独的文件中)定义所有数据
$queries = array(
"bottoms" => "SELECT * FROM bottoms",
"users" => "SELECT * FROM users"
);
$dbr = Database::FetchQueries( $queries );
//display some users
while( $row = mysql_fetch_object( $dbr["apps"]->result ) ){
echo $row->title;
}
//display some bottoms
while( $row = mysql_fetch_object( $dbr["bottoms"]->result ) ){
echo $row->title;
}
您能告诉我您对这种 MVC 方法的看法吗?