What is a PHP Resource type? What is it for?

Asked

Viewed 2,222 times

7

I am new to the PHP language, studying about it, I came across a special type available by the language, the type resource. I found a definition for her:

Resource

A resource is a special variable, which maintains a reference to a external resource. Resources are created and used by special functions.

Source: https://secure.php.net/manual/en/language.types.resource.php

What is a Resource type? What is it for? What is its applicability in practice?

3 answers

7

Resource is one of php’s types, it is used for various purposes like making database connections, opening/manipulating files or even working with streams.

As the manual basically speaks the resorce is something returned through a specific function(it may be the fopen, mysqli_connect etc) which if communicated with something external in other words it is a Handler(manipulator).

List of Resources in PHP

  • rray, a Resource can be a file in a fopen() function for example?

  • @Diegodesouzasilva, a Source cannot be manipulated directly, only through functions. Already noticed qtResources are passed to Mysqli in procedural mode?

  • No, you could give me an example?

3

Well, for me the word Resource (resource) has a very clear meaning and it is somewhat difficult to explain the meaning of a word that is self-explanatory. Perhaps with analogy it becomes clearer what resources are: If you think that a certain thing can provide means or directives to get/get to something else, so just think that these means are nothing more than resources.

So in PHP it really makes sense that a connection link is a resource, a stream/channel of an FTP connection, a file handler created by fopen() or a connection socket. All of them are "points" from which other actions are operated to arrive at something/result.

Resource types were created in the PHP 3 to compensate for the lack of objects, so naturally you could (in fact you can) think that they are analogous to objects where functions returning resources would be object constructors and functions manipulating through the resource would be as methods of these objects.

Still, I don’t understand why the manual refers to itself as a "type of variable" when it’s actually just a special primitive type, but maybe it’s just to make explicit that the variable "remembers" a reference.

0

Resource - According to the official PHP documentation, it is "A feature is a special variable, which maintains a reference to an external resource. Resources are created and used by special functions."

Source http://php.net/manual/en/language.types.resource.php

Existing types of Resource http://php.net/manual/en/resource.php

Some examples

Curl - requests to send and download data (Used generally to communicate with Webservices (SOAP/REST), to download/upload via server;

XML to work with XML in PHP natively;

Imap for e-mail (sending/receiving), handling of electronic mailboxes.

ODBC/mysql/dbase/dba for manipulation of databases, among others.

  • There are several methods associated with each item, just illustrated the name as they are found.

  • Would be Plugins/Libraries the most appropriate names, the so-called PHP extensions, usually configured in php.ini. Usually they already come with PHP, but new and newer can be installed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.