Jump to content

Understanding Resources


Recommended Posts

This is a several part question whose parts relate to the same notion -- the nature of resources. The order in which the questions are posed is important. These questions are based on many minutes of experimentation with various conclusive and inconclusive results.QUESTION ONE: Is it reasonable to assume that a resource is a kind of file version whose content does not change despite changes made to the file through other resources?QUESTION TWO: If the answer to Question One is yes, then is it possible to access a resource after it has been closed?QUESTION THREE: If the response to Question Two is no, why is the pointer contained in the reference variable to a resource retained after the resource has been closed?QUESTION FOUR: If it possible to access the contents of different open resources to the same file , how is it done?Roddy

Link to post
Share on other sites

1. Resources aren't just files. A resource is an internal object-like PHP structure that represents a thing. For example, a MySQL resource represents an opened connection to a MySQL database, ready to receive queries and output results.... Think of it as a black box. Something goes in, something goes out, but you don't know what happens inside.2. Depends on the resource. Usually, no.3. It's possible, but only if both handles are just reading the file. If one is reading, a second one can't start writing. If one is writing, a second one can't start to read or write. "handle" in this case means a pointer to the file from any instance of any program (whether it's PHP or not).

Link to post
Share on other sites
1. Resources aren't just files. A resource is an internal object-like PHP structure that represents a thing. For example, a MySQL resource represents an opened connection to a MySQL database, ready to receive queries and output results.... Think of it as a black box. Something goes in, something goes out, but you don't know what happens inside.
COMMENT: I am only interested in black boxes whose output can be well understood. I need to know what is going to come out when I put something in. I am sure that you can appreciate my concern.QUESTION: Are you saying that a resource is an object that is constructed from the contents of a file, but whose methods and properties are entirely different from those of the file from which it was constructed? If this is true, than I can more easily understand.
2. Depends on the resource. Usually, no.
The word "usually" is not well understood in this context. I can understand that an object exists, but is not available until some action is taken on it. In contrast, I cannot understand that an object simultaneously exists and does not exist. Please clarify.
3. It's possible, but only if both handles are just reading the file. If one is reading, a second one can't start writing. If one is writing, a second one can't start to read or write. "handle" in this case means a pointer to the file from any instance of any program (whether it's PHP or not).
Are you saying that more than one method (function) can point to the same object (resource) at the same time, but that neither method can act on the object while the other method is acting on it?Roddy
Link to post
Share on other sites
COMMENT: I am only interested in black boxes whose output can be well understood. I need to know what is going to come out when I put something in. I am sure that you can appreciate my concern.
Knowing that is part of the reference's job. For example, fread() will accept an integer that is the length of bytes to read from the file, and will output the contents from the current cursor position to that many more bytes.
QUESTION: Are you saying that a resource is an object that is constructed from the contents of a file, but whose methods and properties are entirely different from those of the file from which it was constructed? If this is true, than I can more easily understand.
In the case of resources created with fopen(), I guess you can say that.A file (in general) is just a sequence of bytes, written under a common identifier on non volatile memory (pretty much anything but RAM; e.g. HDD, SSD, CD, etc.). Therefore, the notion of "properties are entirely different from those of the file from which it was constructed" is false by default, since a file has no properties to begin with. It's just bytes. Any methods and/or properties exists only in the language you're reading a file from.In the case of PHP, the resource is like an object that has a pointer to the file (i.e. a pointer to the start of the byte sequence), and the current position within it.
The word "usually" is not well understood in this context. I can understand that an object exists, but is not available until some action is taken on it. In contrast, I cannot understand that an object simultaneously exists and does not exist. Please clarify.
Some functions for freeing resources only close handles, connections, etc., without actually removing the resource itself. Think of it as an object that has its default values for properties, on which you can't do anything (i.e. you'd always have to set something first to do anything). Instead of destroying the object, a freeing function may reset its properties to the default values. On a more concrete example, a function that disconnects from a database may destroy the whole resource, or simply disconnect from the database, but keep the resource in case a new connection is later made.
Are you saying that more than one method (function) can point to the same object (resource) at the same time, but that neither method can act on the object while the other method is acting on it?
Yes... except if both are reading. In that case, both can "act".
Link to post
Share on other sites
I am only interested in black boxes whose output can be well understood. I need to know what is going to come out when I put something in. I am sure that you can appreciate my concern.
I think you are confusing things slightly. You cannot know what happens inside the resources data structure (unless you want to poke around inside the Zend source code) - it is just a representation of a certain external entity. That is the function of black boxes - you don't need to know what happens inside. If you do, then don't use the resource-generating functions that are already available - write your own module that handles files or whatever in a way you can understand. But it's sort of pointless. The way you know how input maps to output is by reading the documentation.Also, resources are not representations of files, and have nothing to do with files in particular - PHP's file management functionality just uses resources to handle the connection. So the answer to question one is no, and therefore two isn't entirely relevant.What makes you think the pointer is retained anyway?
Link to post
Share on other sites
I think you are confusing things slightly. You cannot know what happens inside the resources data structure (unless you want to poke around inside the Zend source code) - it is just a representation of a certain external entity. That is the function of black boxes - you don't need to know what happens inside. If you do, then don't use the resource-generating functions that are already available - write your own module that handles files or whatever in a way you can understand. But it's sort of pointless. The way you know how input maps to output is by reading the documentation.
There is no confusion here. I am perfectly satisfied that I do not know what happens inside a built-in PHP method (function), so long as I know what comes out when I put something in. Obviously, what comes out is not always what I expect, because I do not understand what is going on inside. Unfortunately, at my current level of PHP competence I am still unable to explore the properties and methods of a built-in PHP function with any degree of efficiency.Am I in error to believe that resources are not objects with properties and functions shared by all resources?
Also, resources are not representations of files, and have nothing to do with files in particular - PHP's file management functionality just uses resources to handle the connection. So the answer to question one is no, and therefore two isn't entirely relevant.
Are you familiar with ActionScript? Perhaps you could explain with terminology that I am more familiar like listener objects, event-handlers, event objects, classes, and objects created from classes, etc.
What makes you think the pointer is retained anyway?
Because the value of the reference variable to which it is assigned is still present after the resource has been closed.Roddy
Link to post
Share on other sites

Well, PHP resources are a bit like instances of a class whose members are completely opaque to you. However, resources serve a variety of purposes and so the implementation will always differ in nature.Resources should never be handled directly by your script - you pass them to other functions, which return logical results.

Link to post
Share on other sites
Well, PHP resources are a bit like instances of a class whose members are completely opaque to you. However, resources serve a variety of purposes and so the implementation will always differ in nature.Resources should never be handled directly by your script - you pass them to other functions, which return logical results.
So, they are a kind of event object (ActionScript) -- an object created by one object to carry information to another object. In ActionScript such objects usually contain properties that when received determine the behavior of the recipient object. By way of example, when I apply the fopen( ) function to a file, the fopen( ) function creates an object (a resource) that contains information about the file just acted upon by the fopen( ) function. When I assign the return value of the fopen( ) function to a variable, I am in effect creating a named reference for that object that I can then pass on to other functions as a parameter for further processing. Is this correct?Roddy
Link to post
Share on other sites
Yes. That is correct.
Great! I now have a much better feel for what a resource is.Good team work! Much appreciated.Roddy
Link to post
Share on other sites

I'll throw in my $.02 on a couple points that seemed to generate confusion.

I am perfectly satisfied that I do not know what happens inside a built-in PHP method (function), so long as I know what comes out when I put something in. Obviously, what comes out is not always what I expect, because I do not understand what is going on inside. Unfortunately, at my current level of PHP competence I am still unable to explore the properties and methods of a built-in PHP function with any degree of efficiency.
Methods, functions, objects, and resources are not the same things. Each is distinct. You probably know what a function is, it's just an algorithm that has zero or more inputs, and produces zero or more outputs (to put it vaguely). An object is an instance of a class, and a class is a definition for a data structure composed of both functions and variables. In the context of an object, a function is referred to as a method of the object, and a variable is referred to as a property of the object. A resource is none of those, a resource is most like an object, but not really the same thing. The reason I'm bringing this up is because the phrase "the properties and methods of a function" doesn't make sense, functions have neither properties nor methods.Here are a couple examples of resources that people are most likely to run into:ftp - a connection to an FTP server, an FTP stream (from ftp_connect)gd - an image that PHP is manipulating (from imagecreate, imagecreatefromjpeg, etc)imap - an IMAP connection to a mail server (from imap_open)mysql link - a connection to a MySQL server (from mysql_connect)mysql result - the result from a query (from mysql_query)stream - streams are like pointers to items on the server, such as files, directories, or processes currently running (from opendir, fopen, popen, etc)xml - an XML document structure (from xml_parser_create)You can get a list of all of PHP's resource types here:http://www.php.net/manual/en/resource.phpAn analogy in ActionScript might be when you use a shared object to save data locally, the shared object could be considered a resource for a file on disk.
Link to post
Share on other sites
The reason I'm bringing this up is because the phrase "the properties and methods of a function" doesn't make sense, functions have neither properties nor methods.
If I am to take credit for this remark, then I should apologize. Please consider it a typo.Based upon what you have written below you reject the notion that resources are a kind of event object that transmit information, rather you see them as persistent or temporary objects that can be repeatedly called by other objects to perform a highly specific set of tasks. But then, how are they any different from class objects (instances) created to perform a particular set of tasks? In short, what sets them apart? Their structure? The tasks that they perform? Their accessibility?
Here are a couple examples of resources that people are most likely to run into:ftp - a connection to an FTP server, an FTP stream (from ftp_connect)gd - an image that PHP is manipulating (from imagecreate, imagecreatefromjpeg, etc)imap - an IMAP connection to a mail server (from imap_open)mysql link - a connection to a MySQL server (from mysql_connect)mysql result - the result from a query (from mysql_query)stream - streams are like pointers to items on the server, such as files, directories, or processes currently running (from opendir, fopen, popen, etc)xml - an XML document structure (from xml_parser_create)An analogy in ActionScript might be when you use a shared object to save data locally, the shared object could be considered a resource for a file on disk.
Roddy
Link to post
Share on other sites

Personally, I think as long as you keep learning stuff with a "-like" suffix (i.e. if you're prepared for the fact that there will be some differences you'll later uncover), it's OK to think of stuff in whatever way you like, as long as it's at least close to the real deal... it certainly makes learning easier. Besides, in the end, there's just assembly, in which none of the programming concepts we're talking about exists.I confirmed what you said earlier based on your definition of an event object (and the example), not for the "event" itself. In other words, I'd say that indeed, a resource is like "an object created by one [function] to carry information to another [function]".But again, whether you see resources like an object that's passed around to different functions that may alter it, or like an object from which functions request stuff, it doesn't really matter - none of it is absolutely true, none of it matter anyways... that's the whole idea - it shouldn't matter.

Link to post
Share on other sites
... none of it matter anyways... that's the whole idea - it shouldn't matter.
It sure matters to me. I surely cannot know it all before I begin, but it surely helps to have a good overview before I begin to build. That does mean that I have to know everything about swimming before I get in the water, but I do have to know enough so as not to drown should I find myself slipping into the deep end.Roddy
Link to post
Share on other sites

Yes. It helps to have some overview, but you don't need to bother with details like what calls what. You only need to know that you have a special value generated by a function, used with another function to do something.You only need to know more details if you suddently decide to work with PHP's source.

Link to post
Share on other sites
You only need to know more details if you suddently decide to work with PHP's source.
Yes, it is true one can get carried away in talking about what one should be doing, but I find knowing the vocabulary of the discussion helps enormously in reducing the chatter. Agreed?Roddy
Link to post
Share on other sites

It may help the conversation to realize that in choosing PHP over a less abstracted language like C or Assembly, you are already choosing to work with data structures that you do not fully understand. That is the point. The concept "bit" exists so that you do not have to deal with complex engineering details like voltage. A "byte" exists so that you do not have to deal with ones and zeroes. A string exists so that you do not have to deal with arrays of characters. An identifier (ie, variable name) exists so that you do not have to deal with the memory addresses where characters are stored. A function exists so that you do not have to move bytes from one memory location to another. Objects exist so that you can manipulate isolated chunks of data in a manner that makes sense to your brain (but bears little relationship to actual bits and bytes). A resource exists so that you can ignore the very complex details of moving data from permanent storage into RAM (in the case of file resources) or collating data stored in hundreds or even thousands of discreet files (in the case of a database resource).A PHP string is an artificial data structure that exists for our convenience. PHP is written to create the illusion of understanding what a string really is, when in fact we do not. We are comfortable working with a string object because it resembles the words and characters that we speak and read on paper everyday in our "physical" reality. But that is not what a string is at all. A resource operates the same way. The only real difference is that is bears much less resemblance to a thing that exists in our physical reality. There is nothing more or less mysterious about a resource because every data structure you already "understand" is an illusion.

Edited by Deirdre's Dad
Link to post
Share on other sites
A resource exists so that you can ignore the very complex details of moving data from permanent storage into RAM (in the case of file resources) or collating data stored in hundreds or even thousands of discreet files (in the case of a database resource).
I found this very useful and quite enjoyed what led up to it.
There is nothing more or less mysterious about a resource because every data structure you already "understand" is an illusion.
This left me rolling on the floor. Many thanks for your input!Roddy
Link to post
Share on other sites

A little bit more. If you want a deeper understanding of data structures, get yourself a tiny C compiler that operates in a console window and learn about things like: allocating and deallocating memory blocks, creating character arrays (strings), and pointers, including pointer arithmetic. You will develop a serious respect for all the things that PHP and JavaScript take care of for you. (You won't really, really get it until you have a peek at Assembly, but at least with C you can understand what it is that Assembly is doing for you, if that makes sense.)If you really want to learn about file input and output, change your career plans and become a systems engineer. Even C programmers take advantage of pre-compiled library functions to do that stuff. It's extremely "close to the metal" and I thank my stars that someone else has made the process invisible for me.Ordinary mortals (non-programmers) really have no clue what's going on when they select "Open" on a File menu or tell their iPod to "Play." Seriously, no clue at all. I ask my students to explain the difference between an iPod and a cassette player, and they don't even know where to begin. Maybe one kid out of thirty can cough up the word "digital," and even then the chances of him knowing what that means are very, very small. But they can all run rings around me when it comes to actually using a thing like Facebook.We all operate at some level of abstraction from the things we use daily, I guess. I couldn't repair an automobile engine, for example. But I at least know what combustion means, and how it causes pistons to move. It's not that hard to learn the theory. Digital stuff is a whole other thing. Be glad you're part of the in-crowd.

Edited by Deirdre's Dad
Link to post
Share on other sites
I ask my students to explain the difference between an iPod and a cassette player, and they don't even know where to begin.
I know, I know! You don't have to rewind an iPod!
But they can all run rings around me when it comes to actually using a thing like Facebook.
You know, that frustrates me too. I'm a 30 year old professional developer of web applications, and my ex girlfriends and my mom and sister and other friends who generally don't understand computers any farther than they can throw them are able to do all kinds of things on Facebook, meanwhile I'm sitting there trying to figure out the sorting algorithm for "Top News" (anyone understand that?), or how to find the messages that someone apparently sent me.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...