Recursos

Un valor tipo resource es una variable especial, que contiene una referencia a un recurso externo. Los recursos son creados y usados por funciones especiales. Vea el apéndice para un listado de todas estas funciones y los tipos resource correspondientes.

Vea también la función get_resource_type().

Conversión a recurso

Dado que las variables resource contienen gestores especiales a archivos abiertos, conexiones con bases de datos, áreas de pintura de imágenes y cosas por el estilo, la conversión a tipo resource carece de sentido.

Liberación de recursos

Gracias al sistema de conteo de referencias introducido con el Motor Zend, un valor de tipo resource sin más referencias es detectado automáticamente, y es liberado por el recolector de basura. Por esta razón, rara vez se necesita liberar la memoria manualmente.

Nota: Los enlaces persistentes con bases de datos son una excepción a esta regla. Ellos no son destruidos por el recolector de basura. Vea también la sección sobre conexiones persistentes para más información.

add a note

User Contributed Notes 1 note

up
0
mrmhmdalmalki at gmail dot com
2 months 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