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.
Resource
Recurso é uma variável especial, mantendo uma referência de recurso externo. Recursos são criados e utilizados por funções especiais. Veja o apêndice para uma lista de todas essas funções e seus tipos correspondentes.
Nota: O tipo resource foi incluído no PHP 4.
Veja também get_resource_type().
Convertendo para recurso
Como os tipos resource sustentam manipuladores especiais para arquivos abertos, conexões de bancos de dados, pinceis de desenho e coisas assim, você não pode converter nenhum valor para o tipo resource.
Liberando recursos
Através do sistema de contagem de referências introduzido com o engine da Zend no PHP 4, é automaticamente detectado quando um recurso não mais é referenciado (assim como o Java). Quando isto acontece, todos os recursos em uso por esse resource são liberados pelo coletor de lixo. Por essa razão, é raramente necessário liberar memória manualmente utilizando alguma função free_result.
Nota: Conexões persistentes de bancos são especiais. Eles não são destruídos pelo coletor de lixo. Veja também conexões permanentes.
Resource
07-Jul-2008 11:55
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
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
