Resources are commonly used to iterate through a mysql or file handle.
example
<?php
while($row = mysql_fetch_row($resource)){
echo $row[0] ;
}
?>
It's possible to fake this treatment.
<?php
class fakewhile{
public $arrayCount;
public $arrayCounter;
function setArrValues(){
$this->arrValues = array(0 =>array("apple","artichoke","apricot"),1 => array("bears","dogs","cats"));
$this->arrayCounter = 0;
$this->arrayCount = count($this->arrValues);
}
function outputValues(){
/*
* Anything until the if statement is evaluted one more
* time then the array count value
*/
$arrayInfo = $this->arrValues;
$arrCounter = $this->arrayCounter;
if($arrCounter > $this->arrayCount){
return false;
}
$endCounter = $arrCounter+1;
$this->arrayCounter = $endCounter;
return $arrayInfo[$arrCounter];
}
}
$fw = new fakewhile();
$fw->setArrValues();
while($row = $fw->outputValues()){
print_r($row);
}
?>
Hopefully will get someone started on completing a complete application.
リソース
リソースは特別な変数であり、外部リソースへのリファレンスを保持しています。 います。リソースは、特別な関数により作成され、使用されます。 これらの関数および対応する全てのリソース型の一覧については、 付録 を参照ください。
注意: リソース型は、PHP 4 で導入されました。
get_resource_type() も参照ください。
リソースへの変換
リソース型は、オープンされたファイル、データベース接続、 イメージキャンバスエリアのような特殊なハンドルを保持するため、 他の値をリソースに変換することはできません。
リソースの開放
PHP 4 の Zend エンジンに導入されたリファレンスカウンティングシステムのおかげで、 あるリソースがもう参照されなくなった場合に (Java と全く同様に)、 そのリソースは自動的に削除されます。この場合、このリソースが作成した 全てのリソースは、ガベージコレクタにより開放されます。 このため、free_result 関数を用いて手動でメモリを開放する必要が生じるのはまれです。
注意: 持続的データベース接続は特別で、ガベージコレクタにより破棄されません。 持続的接続 も参照ください。
リソース
wetmonkey__ at at __gmail dot com
16-Dec-2008 08:17
16-Dec-2008 08:17
adrian dot dziubek at gmail dot com
07-Jul-2008 11:55
07-Jul-2008 11:55
I spent an hour trying to create mock setup for testing SQL queries. The explanation here, that a resource contains file handlers and therefore there is no sense in trying to create one is lame. Being unable to redefine functions, creating a fake resource was the second thing I tried to put test in place, but looking at the search results, I see I'm the first one to try... For me it looks like security by obscurity.
evildictaitor at hotmail dot com
17-Aug-2004 01:25
17-Aug-2004 01:25
In response to yasuo_ohgaki, the reason for the inability of the $_SESSION[] variable to hold references is because a session is just a serialize()'d version of it's member variables saved under a unique filename, with this filename following the user around.
$_SESSION[] is therefore limited by the constraints of the serialize() function
Although this is not <i>strictly</i> true, ($_SESSION does some handling to convert messy variables (e.g. "s and ;s)) it cannot store resources due to the serialise() function's dependancy
isaac at chexbox dot com
23-Jun-2002 04:37
23-Jun-2002 04:37
For the the oblivious: An example of a resource would be a mysql database connection.
$result = mysql_connect("localhost", "username", "pass");
//$result variable is a resource.
print $result;
//will print: Resource ID#1, or something similar
