Jump to content

Reflexive Identification


iwato

Recommended Posts

QUESTION: How does one identify a file to itself?BACKGROUND: Recently I stumbled on a very basic but daunting problem: how to specify a file containing a function whose parameter is the file that contains the function? In jQuery this problem is solved with the THIS property. How is it done in PHP?Roddy

Link to comment
Share on other sites

I think you mean reflective identification... anyway, there is a special variable $this in PHP, which is used to refer to the current instance of an object from within that object, the same as it is in JavaScript.

Link to comment
Share on other sites

The __FILE__ constant holds the full path to the current script being executed.
You are a wise fellow, but I must say, this is the first time that I have ever encountered double-adjacent underscores as a definition of anything. This created a significant problem for me. In any case, here is what I discovered after echoing the results from dirname(__FILE__) in the file targeted by the symbolic link.Targeted File Run Independently of the Symbolic Link:This file's filepath: /Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite.Targeted File Run Via the Symbolic Link:This file's filepath: /Applications/MAMP/htdocs/php-practice/functions/filesystem/is_link. Notice that ../fwrite is the folder that contains the file to which the symbolic link points and that ../is_link is the folder that contains the symbolic link generated by the symlink( ) function.So, I opened the file created by the symlink( ) function. All that I found in the file was a relative path to the targeted file -- namely, ../fwrite/fwrite.php.Adding all of this up, does it make sense that the symlink( ) function copies the contents of the file to which it points and replaces itself momentarily with the copied file? I say momentarily, because when I open the symbolic link after the procedure has been run it only contains the aforementioned relative path.Roddy
Link to comment
Share on other sites

Adding all of this up, does it make sense that the symlink( ) function copies the contents of the file to which it points and replaces itself momentarily with the copied file?
That doesn't really make sense, that's not what a symbolic link is. There's more information on this page:http://www.php.net/manual/en/language.cons....predefined.php
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
Link to comment
Share on other sites

That doesn't really make sense, that's not what a symbolic link is. There's more information on this page:http://www.php.net/manual/en/language.cons....predefined.php
Yeah, I had already seen this page. This is how I learned about the term "magical constant" that I used in my reply to Synook. It took me a whlie to find it by the way, as magical constant implies something very special -- more special than just a self-reference. Are you suggesting that the symbolic link is treating my fwrite.php as an include? Surely not.Did you see the note from csaba?Roddy
Link to comment
Share on other sites

Are you suggesting that the symbolic link is treating my fwrite.php as an include? Surely not.
No, I'm saying that the behavior you're seeing is by design. When you access a symbolic link it stores the link in the __FILE__ constant, when you access the file directly it stores the actual filename. That's what it sounds like it's saying it does.
Did you see the note from csaba?
I'm not sure how that applies, what are you referring to?
Link to comment
Share on other sites

No, I'm saying that the behavior you're seeing is by design. When you access a symbolic link it stores the link in the __FILE__ constant, when you access the file directly it stores the actual filename. That's what it sounds like it's saying it does.
I do not understand, but perhaps my most recent experiment will add light on the matter.I changed the $link parameter of the symlink( ) function to ../fwrite/fwrite_3.php.Notice that the folder toward which fwrite_3 is pointed is the same folder in which the targeted file resides -- namely, ../fwrite/fwrite.phpWhen the symbolic link is created in this manner, not only does the PHP script in fwrite.php (the target file) run, but it behaves identically to the way it behaves when the file is accessed independently of the symbolic link.Moreover the file that is created by the symlink( ) function no longer resides in the folder of the file that creates it, rather it lies in the folder toward which it is pointed by the $link parameter. This is probably in no way surprising. What is surprising, though, is that the link contained in the symbolic link is written as if the newly created symbolic link resided in the same folder as the file that created it -- namely, ../fwrite/fwrite.php.Can you offer a clear explanation for what is going on. I am totally baffled.Roddy
Link to comment
Share on other sites

I'm not sure why all of what you've experienced has happened, but there's something about symlinks that you should know (in case you don't already) - they are being written literally.When you create a symlink like

symlink('../fwrite/fwrite.php', '../../thisIsASymbolicLink');

The path in the second argument is resolved, and at that location, the first argument is written literally, with no kind of resolution whatsoever.So, if the CWD of your PHP script is, say, "/Applications/MAMP/htdocs/php-practice/functions/", you might expect a link at"/Applications/MAMP/htdocs/thisIsASymbolicLink"that points to"/Applications/MAMP/htdocs/php-practice/fwrite/fwrite.php"but what you get instead instead is a link that when followed resolves to"/Applications/MAMP/fwrite/fwrite.php"Sometimes though, programs resolve links based on their own CWD, and not the links' locaion (I think that's a bug...), which leads to unexpected and odd results.If you want to get a symlink that points to a location having the CWD as a base directory, I suggest you use realpath() in order to make the final file path absolute, like:

symlink(realpath('../fwrite/fwrite.php'), '../../thisIsASymbolicLink');

Link to comment
Share on other sites

If you want to get a symlink that points to a location having the CWD as a base directory, I suggest you use realpath() in order to make the final file path absolute, like:
symlink(realpath('../fwrite/fwrite.php'), '../../thisIsASymbolicLink');

I do not do this very often, but this time I must surrender and move on. There is too much in PHP that I have yet to learn, and I cannot afford to spend any more time on this function. In the end, I have even deviated from the original topic -- namely, reflexive identification.Thank you everyone for your kind help, but the particulars of symlink( ) must wait until I have found a true need for it. Already I have a basic understanding of how it can be used and will worry more about how it actually works when I truly need to apply it.Thank you Boen_Robot for introducing me to the realpath( ) function, and now I must return to realpath(Roddy).Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...