Laravel Live Denmark 2026

リソース

resource は特別な変数であり、外部リソースへのリファレンスを保持しています。 リソースは、特別な関数により作成され、使用されます。 これらの関数および対応する全ての resource 型の一覧については、 付録 を参照ください。

get_resource_type() も参照ください。

リソースへの変換

resource 型は、オープンされたファイル、データベース接続、 イメージキャンバスエリアのような特殊なハンドルを保持するため、 他の値を resource に変換することはできません。

リソースの開放

Zend エンジンの一部であるリファレンスカウンティングシステムのおかげで、 ある resource がもう参照されなくなった場合に (Java と全く同様に)、 そのリソースは自動的に削除されます。この場合、このリソースが作成した 全てのリソースは、ガベージコレクタにより開放されます。 このため、free_result 関数を用いて手動でメモリを開放する必要が生じるのはまれです。

注意: 持続的データベース接続は特別で、ガベージコレクタにより破棄されません。 持続的接続 も参照ください。

add a note

User Contributed Notes 1 note

up
0
mrmhmdalmalki at gmail dot com
1 hour 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