Jump to content

The SPL Iterator


iwato

Recommended Posts

COMMENT: For good or for bad I have discovered the Standard PHP Library (SPL). The good is that I do not have to reinvent the wheel every time I have a neat idea that I do not know how to realize. The bad is that the people who write these extensions are no dummies, and I am still a beginner.QUESTION: Could someone please explain in detail the condition of the if statement in the block of code below. Perhaps, if I could understand it, the rest of the block fall into place.

<?php	$iterator = new DirectoryIterator(dirname(__FILE__));	foreach ($iterator as $fileinfo) {		if (!$fileinfo->isDot()) {			$octal_perms = substr(sprintf('%o', $fileinfo->getPerms()), -4);			echo $fileinfo->getFilename() . ": " . $octal_perms . "<br />";		}	}?>

Roddy

Link to comment
Share on other sites

Well... it has to do with how the files and folders (or directories) are represented in UNIX-like operating systems. Basically, there's the ls command, which lists all the files and directories in the current directory. Executing it outputs something like

.	 ..	 image1.gif	 image2.png	 php_folder

And there's the cd command. If you were to execute, say

cd ..

you would go to the current directory's parent. cd . doesn't take you anywhere. So if you were somewhere like

/etc/private/php/

and executed

cd ..

you would now be here

/etc/private/

This being said, the loop in your code goes through every file in the specified directory. Those are not all files, because it also goes through . and .. (which are more like... location aliases). The isDot() method checks to see if the current item is a . or .. If it isn't (notice the ! in the IF clause), the current file in the iterator is printed, along with some other info generated by the line above.It might not be that clear, but at least I hope you get the big picture.

Link to comment
Share on other sites

It might not be that clear, but at least I hope you get the big picture.
The part of your discussion dealing with the . and .. led me to the conclusion that isDot( ) could be a method of the DirectoryIterator class -- certainly, it was not in the list of standard PHP functions. Having discovered this to be true further exploration led me to more sample code and a discussion of the the Scope Resolution or paamayim-nekudotayim operator. From this discussion I was able to deduce that the -> operator is used to access methods of a class from objects that are created from that class. This latter discovery led to my understanding of the condition of the if statement, and as hoped everything else fell into place.I would now like that someone answer true or false to the following two questions and provide a brief explanation as to why you answered as you did.1) There are two ways to access the publicly defined methods and properties of a class from outside a class definition. One can use the -> operator in conjunction with an instance of the class that contains the method or property. Or, one can use the :: operator in conjunction with the name of the class.2) In a UNIX directory path the dot and double-dot can be used interchangeably.I have one final question, and it is short-answer. What is the name of the -> operator? I am asking this because I was unable to find any direct reference to it in the online PHP User's Manual.Roddy
Link to comment
Share on other sites

The part of your discussion dealing with the . and .. led me to the conclusion that isDot( ) could be a method of the DirectoryIterator class -- certainly, it was not in the list of standard PHP functions. Having discovered this to be true further exploration led me to more sample code and a discussion of the the Scope Resolution or paamayim-nekudotayim operator. From this discussion I was able to deduce that the -> operator is used to access methods of a class from objects that are created from that class. This latter discovery led to my understanding of the condition of the if statement, and as hoped everything else fell into place.
These are quite the basics of OOP, you should have a very good understanding of them before trying something like the SPL.
1) There are two ways to access the publicly defined methods and properties of a class from outside a class definition. One can use the -> operator in conjunction with an instance of the class that contains the method or property. Or, one can use the :: operator in conjunction with the name of the class.
1 and 0, yes and no, true and false :) The :: operator is used for static methods. So you'd have something like this
class Person {	static function PersonSaysSomething() {			echo '<p>Hello, World!</p>';	}}

Inside a static function you can't include variables. So the function always has the same effect. If you don't define it as static, you won't be able to use it using ::

2)  In a UNIX directory path the dot and double-dot can be used interchangeably.

0, no, false. The . is an alias for the current directory, while .. is an alias for the current directory's parent. So let's say we are here

/etc/private/php

We list all the files and directories in the current (php) directory

ls -a

This outputs

.		..		file1		file2		directory1		directory2

If we change the directory using the . alias

cd .

and print the current working directory

pwd

the output is

/etc/private/php

which is the same directory we were previously in. If we now change the directory using the .. alias

cd ..

and print thte current working directory

pwd

the output is

/etc/private

because going to the .. alias moves us one directory up. So no, they are no interchangeable.

I have one final question, and it is short-answer. What is the name of the -> operator? I am asking this because I was unable to find any direct reference to it in the online PHP User's Manual.
I'm not sure it has a name, in Programming PHP, for example, it says
Once you have an object, you can use the -> notation to access methods and properties of the object
Link to comment
Share on other sites

If you don't define it as static, you won't be able to use it using ::
If this statement were true, then how would you go about explaining that the statement B::showclass( ); below returns A? Is it not true that an unspecified function is considered public by default?
class A {	function showclass() {		echo __CLASS__;	}}class B extends A {}B::showclass();

The . is an alias for the current directory, while .. is an alias for the current directory's parent.
I am not a UNIX person, and as an Apple user I think in terms of folders, files, and paths. For example, I understand that the relative path "../folder1/file.ext" refers to a file in a folder that is contained in the same folder in which my current folder is in. This is the only use of .. with which I am familiar. Can you explain the use of a single dot within this context?Roddy
Link to comment
Share on other sites

Just execute the commands I've written in Apple's Terminal (/Applications/Utilities/Terminal.app). I'm also an OS X user, as my signature says.The code you provided would work, but it would produce a strict standards error. Using a variable inside the showclass() method would produce a fatal error.

Link to comment
Share on other sites

If this statement were true, then how would you go about explaining that the statement B::showclass( ); below returns A? Is it not true that an unspecified function is considered public by default?
class A {	function showclass() {		echo __CLASS__;	}}class B extends A {}B::showclass();

I think you're confusing "public" with "static" here. Yes, without a visibility keyword (public, private or protected), class members are public. But without the "static" keyword, members are not static.Being static means that you are allowed to call the method without creating the object... essentially the same thing as if using a plain function, with the only difference that you specify "classname::" when you call the function.Being public means that the method can be called from outside the class. Whether you call it statically or not is irrelevant.Your sample code works because PHP is very fault tolerant. The only it does to promote better practices is the strict warning 23.12.2012 talks about.
I am not a UNIX person, and as an Apple user I think in terms of folders, files, and paths. For example, I understand that the relative path "../folder1/file.ext" refers to a file in a folder that is contained in the same folder in which my current folder is in. This is the only use of .. with which I am familiar. Can you explain the use of a single dot within this context?Roddy
MAC OS X is based on UNIX... so... that's like a Windows 98 person saying he isn't a DOS user... such a thing doesn't exist by definition. If you're not comfy with the "under the hood" terminology, that's another issue.The ".." and "." are only useful in cases where a relative path is required. In absolute paths, they both make little sence. Using "." in an absolute path is basically like using nothing. e.g.
/something/./somethingElse

is equivalent to

/something/somethingElse

And 23.12.2012 already gave a sample of what ".." does.So... if "." is equivalent to having nothing, why have it in the first place you may ask? Every program resolves relative paths against a certain base directory (called the "current working directory" in most cases). Suppose you wanted to perform an action against whatever that directory is. Specifying an empty string as a path doesn't work (I think...), but specifying "." explicitly targets that directory.

I have one final question, and it is short-answer. What is the name of the -> operator? I am asking this because I was unable to find any direct reference to it in the online PHP User's Manual.
In the list of parser tokens, they mention it as "object operator". I suppose that's a good way of putting it.
Link to comment
Share on other sites

By the way, Apple is based on Unix... but all operating systems of any note use the same file path notations (except for Windows, with its "drive letters").The single dot refers to the current directory. Consider two different programs:

$ ls /binprogram$ lsabc  program$ programprogram in /bin run$ ./programprogram in current directory run

Link to comment
Share on other sites

UNIX '03 compliant, to add to that :)Nonetheless, you'll find out that it is less about learning the language and more about learning techniques. The piece of code in the first post is more of a technique. The loop gets the output of a ls -a command, and the conditional filters the dots, which would be irrelevant to the user. And this is the way described even in the manual. One way to figure things out is to either read the manual page, which explains everything you need to know, or simply remove the conditional and see the output.

Link to comment
Share on other sites

Just execute the commands I've written in Apple's Terminal (/Applications/Utilities/Terminal.app).
This was a very helpful instruction. I have now reproduced what you indicated and have convinced myself that . and .. are not interchangeable. Perhaps it is now obvious that I am not accustomed to using Apple's Terminal application. This said, are the following correct interpretations of the commands that you provided:cd [folder_name] = change directory to [folder_name]cd .. = move to next folder upstream.cd . = remain in the current folderls = listls -a = list all visible and invisible folders and files in the current folderpwd = show the upstream path from the current folder
I'm also an OS X user, as my signature says.
The difference is that you have spent more time on the UNIX side of the wall that separates our mutually shared MacOS X garden. I rarely climb the wall unless I am told to do so.
The code you provided would work, but it would produce a strict standards error.
No error is produced. Not even a warning or notification. It simply works.
Using a variable inside the showclass() method would produce a fatal error.
This I have yet to try.Roddy
Link to comment
Share on other sites

No error is produced. Not even a warning or notification. It simply works.
You need to turn on strict error reporting (change the error_reporting value to E_ALL | E_STRICT). Then you will see:
Strict Standards: Non-static method A::showclass() should not be called statically in /path/static.php on line 9A
Note that strict errors need to be enabled in the PHP configuration file, as they are triggered before a custom error reporting level can be set at runtime.
Link to comment
Share on other sites

By the way, Apple is based on Unix... but all operating systems of any note use the same file path notations (except for Windows, with its "drive letters").
This is comforting to know.
The single dot refers to the current directory.
I understand this now. My thanks go to 23.12.2012 and boen_robot for their patience and persistence. This said, the following leaves me completely baffled. Is this in a language that I should know?
$ ls /binprogram$ lsabc  program$ programprogram in /bin run$ ./programprogram in current directory run

Link to comment
Share on other sites

One way to figure things out is to either read the manual page, which explains everything you need to know, or simply remove the conditional and see the output.
If this had been true, this topic would never have been.In any case, many thanks.An important door, has been opened very quickly.Roddy
Link to comment
Share on other sites

This said, the following leaves me completely baffled. Is this in a language that I should know?
They represented commands, as typed in a terminal. The $ sign indicates we are running unprivileged - you should have seen it to the left of your cursor when you opened up the terminal earlier. The ls command will give you a list of files and folders in a directory, and you can type program names to run them. However, by default, the terminal will first look in some predefined directories (such as /bin) for binaries to run. So if you wanted to run a program that resided in your current directory, but had a name identical to that in one of the predefined binary directories, then we would need to be explicit, by using the current folder symbol.Edit: try it!And whoops, I actually meant OS X is based on Unix. Apple Inc., the company, has no corporate relation to the developers of the original Unix systems.
Link to comment
Share on other sites

You need to turn on strict error reporting (change the error_reporting value to E_ALL | E_STRICT). Then you will see:
Yes, thanks. Now I can see the following error message:Strict Standards: Non-static method A::showclass() should not be called statically in blah,blah,blah on line blah, blah, blah.
Note that strict errors need to be enabled in the PHP configuration file, as they are triggered before a custom error reporting level can be set at runtime.
By running the error_reporting( ) function with the $level parameter set to E_STRICT or -1, I am able to reproduce the error at run time. I can achieve the same result by using the set_ini( ) function as well. Both of these changes are only temporary, however.When I open my MAMP PHP configuration file I find the following:
error_reporting  =  MAMP_error_reporting_MAMP

It appears that the people at MAMP Pro have control over the way PHP reports my errors.Roddy

Link to comment
Share on other sites

Edit: try it!
I think, I will pass this time around, as I already understand well the placeholder function of the . parameter.Have a good day!Roddy
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...