PHP 8.5.3 Released!

Recursos

Um resource é uma variável especial, que mantém uma referência a um recurso externo. Recursos são criados e usados por funções especiais. Veja o apêndice para uma lista de todas essas funções e seus tipos resource correspondentes.

Veja também a função get_resource_type().

Convertendo para recurso

Como as variáveis resource mantém manipuladores especiais para arquivos abertos, conexões de bancos de dados, canvas de imagens e coisas do tipo, converter para resource não faz sentido algum.

Liberando recursos

Graças ao sistema de contagem de referência introduzido com a Engine da Zend, um resource sem referências é detectado automaticamente, e liberado pelo coletor de lixo. Por esta razão, é rara a necessidade de liberação de memória manualmente.

Nota: Conexões persistentes de bancos são exceções à regra. Elas não são destruídas pelo coletor de lixo. Veja também a seção conexões persistentes para mais informações.

adicionar nota

Notas de Usuários 1 note

up
0
mrmhmdalmalki at gmail dot com
1 day ago
The resource is not a type you can create; it is a PHP internal type that PHP uses to refer to some variables.

For example, 

You have a number 5.5, which is a Float, and you can convert it to an int type, but you cannot convert it to a resource.

A resource type is used for external resources, like files or database connections, as shown below:

<?php

// a normal variable
$file_resource = fopen("normal_file.txt", "r");

// after we assigned an external file to that variable using the function fopen,
// PHP starts dealing with the file, and since it is an external resource,
// PHP makes the type of the variable a resource that refers to that operation

var_dump($file_resource);

// the previous var_dump returned bool(false),
// because there was no file that PHP could open.
// and it gave me this warning:
// PHP Warning: fopen(normal_file.txt): Failed to open stream: No such file or directory

// now I am trying to open an existing file
$file_resource = fopen("existed_file.txt", "r");

// then check the type:
var_dump($file_resource);

// it gives this:
// resource(5) of type (stream)
// now the variable's type is resource

?>
To Top