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

5

I'm new to the PHP language, studying it, I came across a special type available through the language, type resource . I found a definition for it:

  

Resource

     

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

Source: link

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

    
asked by anonymous 05.10.2015 / 19:08

3 answers

6

resource is of the php types, it is using for various purposes how to make connections with database, open / manipulate files or even work with streams.

As the manual basically talks about resorce is something returned through a specific function (it can be fopen , mysqli_connect etc) that if communicated with something external in other words it is a handler.

List of resources in PHP

    
05.10.2015 / 19:18
1

Well, for me the word resource has a very clear meaning and it is a bit difficult to explain the meaning of a word that is self-explanatory. Maybe with an analogy, let it be clear what it is: If you think that a certain thing can provide means or directives to get / reach another thing, then just think that these means are nothing more than resources.

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

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

    
05.10.2015 / 19:40
0

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

Source link

Existing resource types link

Some examples

Curl - makes requests for sending and downloading data (Usually used to communicate with Webservices (SOAP / REST) p>

XML to work with XML in PHP natively;

Imap for email (sending / receiving), manipulation of electronic mailboxes.

ODBC / mysql / dbase / dba for manipulating databases, among others.

    
05.10.2015 / 19:30