首先先強調一點:Flash Remoting使用HTTP,HTTP是一個Stateless Protocol,所以它不會記住之前的東西,所以有人以為可以這樣做: class test {
function test() {
// Method Table從略...
}
function setVar($value) {
$this->myVar = $value;
}
function getVar() {
return $this->myVar;
}
} 先用setVar儲下數值後,之後可以用getVar取回數值,是沒有可能的;因為每Call一次Method,一個新Object Instance是會產生。
正常做法,當然是利用Session,AMFPHP 1.0開始,第一次連接一個Class時,已經自動處理了Session,不用再用session_start()。另外,在一個純Flash應用中,不用擔心Flash處理不到Session,當包著SWF的HTML(PHP)載入時,Session已經可以產生(假設Cookie沒有被Disabled)。當PHP通過AMFPHP找不到Session時,它才會產生新的Session(例如Cookie被Disabled),SessionID會通過URL去傳送,所以連去Remoting Gateway時,在NetConnection Debugger,可以看到:
...gateway.php?PHPSESSID=6362...
回到正題,可以使用Session保持Persistence時,例如: class test {
function test() {
// Method Table從略...
}
function setVar($value) {
$_SESSION['myVar'] = $value;
}
function getVar() {
return $_SESSION['myVar'];
}
} 我發現一個奇怪的現象,如果一個Session Value在Constructor時已經產生,例如:class test {
function test() {
// Method Table從略...
$_SESSION['myVar'] = "constructor";
}
function setVar($value) {
$_SESSION['myVar'] = $value;
}
function getVar() {
return $_SESSION['myVar'];
}
} setVar無法覆蓋該Session Value,getVar傳回的結果是"constructor",有誰知道為什麼?(原因已經知道,自己真蠢,哈哈,前面已經解釋了)
Persistence有了點小小的眉目,下一步就是想想怎樣做O/R Mapping...
P.S. 發現Openamf有一個有趣範例,它的Persistence是怎樣實現?Add Person後,關閉Browser,再打開,搜尋該記錄,仍然存在!(沒有用Database)
其他參考資料 - Hibernate - Hibernate is a powerful, ultra-high performance object/relational persistence and query service for Java. Hibernate lets you develop persistent classes following common Java idiom - including association, inheritance, polymorphism, composition and the Java collections framework. The Hibernate Query Language, designed as a minimal object-oriented extension to SQL, provides an elegant bridge between the object and relational worlds. Hibernate also allows you to express queries using native SQL or Java-based Criteria and Example queries.
- Propel - Propel is a full-service object persistence and query toolkit for PHP. It allows you to access your database using a set of objects, providing a simple API for storing and querying data. You might already have heard of this technique, but under a different name, like Data Access Objects (DAO) or Object Relational Mapping (ORM).
- PHPObject - With PHPObject, you can call a method of a PHP class/library on your web server as if the class/library was defined in Flash itself. It takes care of your client-server connections and makes passing of variables (properties) between Flash MX and PHP easy, and thereby providing a convenient way to connect rich media clients with data and business logic residing on your server.
|