thorpe wrote:Post your existing code.
I am new to PHP5. I am not sure about the structure and modularity of this code.
please help me out by pointing such mistakes and how to make the code more effectvie. Is it an effective way to create each object for each file type? Can u suggest an alternate way?
I am posting this small code. Please point out the mistakes..
<?
class RegexFilter extends FilterIterator
{
protected $regex;
public function construct(Iterator $it, $regex)
{
parent::construct($it);
$this->regex=$regex;
}
public function accept()
{
return preg_match($this->regex, $this->current());
}
}
class My_Filter
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function Iterator()
{
$info = pathinfo($_SERVER['PHP_SELF']); //reading the current directory
$path=$info[dirname];
$dir = new DirectoryIterator($path); //creating the directory iterator
//$a = end(explode('.',$file4)); //fetches the file type
$filtered_dir = new RegexFilter($dir,'/'.$this->type.'/'); //creating the regexfilter class
foreach($filtered_dir as $file)
{
if(is_dir($file))
{
continue; // avoids the folders
}
print $file."<br>"; //printing the files
}
}
}
$my_filter = new My_Filter('php');
$my_filter->Iterator(); // prints all the php files
$my_filter = new My_Filter('html'); // prints the html files
$my_filter->Iterator();
?>