PHP Doku:: Ressourcen - language.types.resource.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:

Ein Service von Reinhard Neidl - Webprogrammierung.





Eine resource ist eine spezielle Variable, die eine Referenz zu einer externen Ressource darstellt. Ressourcen werden mit Hilfe spezieller Funktionen erzeugt und genutzt. Im Anhang finden Sie eine Liste all dieser Funktionen und der zugehörigen resource-Typen.

Hinweis: Der resource-Typ wurde in PHP 4 eingeführt.

Siehe auch get_resource_type().

Konvertierung von Ressourcen

Da resource-Variablen spezielle Referenzen auf geöffnete Dateien, Datenbankverbindungen, Grafikbereiche usw. enthalten, ist die Konvertierung von resource nicht sinnvoll.

Resourcen freigeben

Dank der - mit der Zend Engine von PHP 4 eingeführten - Referenzzähler werden Ressourcen, die von keiner Variablen mehr referenziert werden, automatisch erkannt und vom Garbage Collector freigegeben. Aus diesem Grund ist es selten nötig, Speicher von Hand freizugeben.

Hinweis: Persistente Datenbankverbindungen sind eine Ausnahme von dieser Regel, sie werden nicht vom Garbage Collector entfernt. Mehr Informationen finden sie im Abschnitt Persistente Verbindungen.

5 BenutzerBeiträge:
- Beiträge aktualisieren...
Soos Gergely
26.04.2010 14:24
It is always wrong to assume that some operation, like casting to a resource makes no sense. People will always find some extreme case where it would be useful; like the previous example with mysql. My problem was that I wanted to start daemons from a web interface but the apache filehandles were inherited which caused that apache was unable to restart. If I could only typecast a number to a filehandle and then close it... Instead I had to write a small C program that closes every filehandle and then starts my program. I surely miss Apache2::SubProcess from perl. (Also, in perl you can reopen a file and then close it using IO::Handle module's fdopen. I'm just saying.)
wetmonkey__ at at __gmail dot com
16.12.2008 1:17
Resources are commonly used to iterate through a mysql or file handle.

while($row = mysql_fetch_row($resource)){
$row[0] ;

It's possible to fake this treatment.

class fakewhile{


$this->arrValues = array(0 =>array("apple","artichoke","apricot"),1 => array("bears","dogs","cats"));
$this->arrayCounter = 0;
$this->arrayCount = count($this->arrValues);

 * Anything until the if statement is evaluted one more
 * time then the array count value
$arrayInfo = $this->arrValues;
$arrCounter = $this->arrayCounter;
$arrCounter > $this->arrayCount){
$endCounter = $arrCounter+1;
$this->arrayCounter = $endCounter;


$fw = new fakewhile();
$row = $fw->outputValues()){

Hopefully will get someone started on completing a complete application.
adrian dot dziubek at gmail dot com
7.07.2008 17: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
16.08.2004 19: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
22.06.2002 22: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

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)