我认为你的回答2和4是有矛盾的,如果如4的回答,那么在2中的现象就正常了,因为BaseClass都消亡了,那么在FacClass中的$instance的指向也就成了个问题。 ------------------------------One user may have sevaral instances.
我认为你的回答2和4是有矛盾的,如果如4的回答,那么在2中的现象就正常了,因为BaseClass都消亡了,那么在FacClass中的$instance的指向也就成了个问题。 ------------------------------One user may have sevaral instances. ================================================== 我觉得还是解释不通的
class DBConnection { //@#$------------------------------------------------------ //| class instance,singleton pattern //@#$------------------------------------------------------ private static $instance;
//@#$------------------------------------------------------ //| database link resource //@#$------------------------------------------------------ private $dbLink;
//@#$------------------------------------------------------ //| connect to database,all paramter are defined in //| application's Config.php file //@#$------------------------------------------------------ private function __construct() { try { $this->dbLink= @mysql_connect(DB_SERVER,DB_USERID,DB_PASSWORD) or throwException("Connect to database failed!"); $error="Database error : Unknown database name"; @mysql_select_db(DB_CATALOG,$this->dbLink) or throwException($error); } catch(Exception $e) { throwException($e); } }
//@#$------------------------------------------------------ //| using singleton pattern for create database connection //@#$------------------------------------------------------ public function singleton() { if(isset(self::$instance)) { return self::$instance; } $class=__CLASS__;
return new $class; }
//@#$------------------------------------------------------ //| execute sql with DBResultset object return //@#$------------------------------------------------------ public function query($query) { $result = @mysql_query($query, $this->dbLink); if($result==false) { throwException("SQL:$query,".mysql_error()); return null; } return new DBRecordset($result); }
//@#$------------------------------------------------------ //| update record //@#$------------------------------------------------------ public function update($query) { $this->execute($query); return mysql_affected_rows(); }
//@#$------------------------------------------------------ //| create a new record of a table and return the insert id //| if error occured will throw a database exception //@#$------------------------------------------------------ public function insert($query) { $this->execute($query); return mysql_insert_id(); }
//@#$------------------------------------------------------ //| delete record,simple call update() //@#$------------------------------------------------------ public function delete($query) { return $this->update($query); }
//@#$------------------------------------------------------ //| get first row's first field value //@#$------------------------------------------------------ public function getOne($query) { $result = @mysql_query($query, $this->dbLink); if($result==false) { throwException("SQL:$query,".mysql_error()); return null; } $row=mysql_fetch_row($result); mysql_free_result($result); return $row[0]; }
//@#$------------------------------------------------------ //| get first row //@#$------------------------------------------------------ public function getRow($query) { $result = @mysql_query($query, $this->dbLink); if($result==false) { throwException("SQL:$query,".mysql_error()); return null; } $row=mysql_fetch_assoc($result); mysql_free_result($result); if($row) return new DBRecord($row); else return null; } } ?>
代码重用,相关的错误处理也好弄
2 这儿的static $instance;是来保证当前用户可以不用多建立很多这个类的实例
4 unset或者页面执行完毕
5 Java了解不多,不过我认为MVC还是有它的好处的,这样开发的程序结果清楚,
方便扩展。
------------------------------One user may have sevaral instances.
echo '$instance: ';
var_dump($instance);
来看一下,无论你调用多少次这个函数,$instance都为:NULL.
如果这个函数的原型为function getDatabaseConnection()而不是function &getDatabaseConnection()的话,第二次调用的时候,$instance就不为空了。
问题是来自于引用,而不是来自static。所以,static在php和java里面的工作方式应该都是一样的。4,显示的释放unset($obj);或者碰到本页面的"?>"标记。你可以在类里面写个析构函数__destruct(),然后测试一下就知道了。5,MVC模式只能说你想不想用,而不是能不能用。说实话我在做PHP开发的时候不喜欢MVC,可能是因为习惯问题吧。不过MVC的好处我们都是知道的。
------------------------------One user may have sevaral instances.
==================================================
我觉得还是解释不通的
多谢你的回答,我还有两个问题请教一下,
1、如果如你所言,“如果这个函数的原型为function getDatabaseConnection()而不是function &getDatabaseConnection()的话,第二次调用的时候,$instance就不为空了”,那么是不是意味着,如果改成function getDatabaseConnection(),那么一个客户在访问期间只需要连接一次数据库就可以了,其他任何时候,都会直接使用这个$instance了。如果是这样,那么为什么xoops采用的是引用方式,而不是你所说那种方式呢?2、想知道你在使用php开发过程中,不采用mvc,一般会采用什么模式呢,我觉得直接用两层可能会比较理想,也就是一个显示层,一个数据操作层,显示层直接调用数据操作层就行了,这样相当于在mvc和普通的页面直接访问数据库两种方式间进行了个折衷,不知道你有何高见?3、(多问一个)你最近有无换工作的打算,如果是北京的,我有个机会推荐给您,公司实力没得说,关键是您的php水平要高,呵呵!
xoops采用这种模式倒可以理解,我不太理解的是,他为什么非要这样定义一个$instance呢,大家认为他这样作到底出于什么目的呢?
import("Kit.DB.DBRecordset");
class DBConnection
{
//@#$------------------------------------------------------
//| class instance,singleton pattern
//@#$------------------------------------------------------
private static $instance;
//@#$------------------------------------------------------
//| database link resource
//@#$------------------------------------------------------
private $dbLink;
//@#$------------------------------------------------------
//| connect to database,all paramter are defined in
//| application's Config.php file
//@#$------------------------------------------------------
private function __construct()
{
try
{
$this->dbLink=
@mysql_connect(DB_SERVER,DB_USERID,DB_PASSWORD)
or throwException("Connect to database failed!");
$error="Database error : Unknown database name";
@mysql_select_db(DB_CATALOG,$this->dbLink)
or throwException($error);
}
catch(Exception $e)
{
throwException($e);
}
}
//@#$------------------------------------------------------
//| using singleton pattern for create database connection
//@#$------------------------------------------------------
public function singleton()
{
if(isset(self::$instance))
{
return self::$instance;
}
$class=__CLASS__;
return new $class;
}
//@#$------------------------------------------------------
//| execute sql with DBResultset object return
//@#$------------------------------------------------------
public function query($query)
{
$result = @mysql_query($query, $this->dbLink);
if($result==false)
{
throwException("SQL:$query,".mysql_error());
return null;
}
return new DBRecordset($result);
}
//@#$------------------------------------------------------
//| execute sql(INSERT,UPDATE,DELETE)
//@#$------------------------------------------------------
private function execute($query)
{
$result = @mysql_query($query, $this->dbLink);
if($result==false)
{
throwException("SQL:$query,".mysql_error());
return 0;
}
}
//@#$------------------------------------------------------
//| update record
//@#$------------------------------------------------------
public function update($query)
{
$this->execute($query);
return mysql_affected_rows();
}
//@#$------------------------------------------------------
//| create a new record of a table and return the insert id
//| if error occured will throw a database exception
//@#$------------------------------------------------------
public function insert($query)
{
$this->execute($query);
return mysql_insert_id();
}
//@#$------------------------------------------------------
//| delete record,simple call update()
//@#$------------------------------------------------------
public function delete($query)
{
return $this->update($query);
}
//@#$------------------------------------------------------
//| get first row's first field value
//@#$------------------------------------------------------
public function getOne($query)
{
$result = @mysql_query($query, $this->dbLink);
if($result==false)
{
throwException("SQL:$query,".mysql_error());
return null;
}
$row=mysql_fetch_row($result);
mysql_free_result($result);
return $row[0];
}
//@#$------------------------------------------------------
//| get first row
//@#$------------------------------------------------------
public function getRow($query)
{
$result = @mysql_query($query, $this->dbLink);
if($result==false)
{
throwException("SQL:$query,".mysql_error());
return null;
}
$row=mysql_fetch_assoc($result);
mysql_free_result($result);
if($row)
return new DBRecord($row);
else
return null;
}
}
?>
但是在同一个页面里,有多个该类的实例时,可以通过static来共享这个 $instance .
这样做可以方便代码的重用和移植,也便于减少更改数据库时的修改量2、在FacClass中定义的static $instance;和java中的static的工作方式好像不一样阿,我试验过程中,还作了分页,发现在翻页过程中,每次在页面上都会打印出"Need Connect"(在FacClass.php文件中),那么定义这个static还有何意义呢?
没有意义!
这个static只是在于:如果数据库已经连接就不再连接,而php本身就提供了这个功能3、在此方式的基础上,我有无可能在BaseClass中,定义一个全局的(类似于Application)的连接对象,然后所有的数据处理都用这个连接对象呢。如果可行,那么也似乎就不必考虑mysql_connect和mysql_pconnect了。
不可以4、在这种方式下,我想知道FacClass中new的BaseClass的生命周期是什么,是show.php页面全部执行完后就结束了吗?
当FacClass的实例注销时
任何变量都只生存在当前执行的程序中5、扩展问题,php中进行MVC模式的设计有没有真正的价值,发现现在的讨论没有什么定论,有的说有价值,有的说根本无法达到java中类似struts的方式,相反在M层可能会由于数据的保持更浪费资源
MVC是思路(方法),不存在“价值”问题。
struts是框架而不是模式,请注意他们的区别