PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm trying to build my first PHP Class. After days of tweaking, im lost. I am used to working with functions and arrays, but wrapping them in classes is confusing me.
The following code is to return an Array Collection representative of a folder and sub folder in a directory of choice. It doesn't work. Throws an error "Call to undefined function: parse_dir()". It would be great if someone out there could see where im not correct.
I've been trying to follow the example provided at [url]http://www.sephiroth.it/tutorials/flashPHP/flex_remoteobject/page003.php[/url]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
i have made some updates from suggestions and still get errors. Here is new code.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
new code,
<?
class Tree {
var $folders;
var $files;
var $name;
// explicit actionscript package
var $_explicitType = "tutorials.Tree";
function Tree($folders, $files, $name)
{
$this->folders = $folders;
$this->files = $files;
$this->name = $name;
}
}
class DirTree {
/**
* Get a tree of folders and files from a spec dir
* @returns An ArrayCollection of Tree
*/
function DirTree( $dir_tree ){
$t = array();
$_tree = $this->parse_dir($dir_tree);
for($a = 0; $a < count($_tree); $a++){ $t[] = new Tree($_tree[$a][0], $_tree[$a][1], $_tree[$a][2]); }
return $t;
}
/**
* Get a tree of folders and files from a spec dir
* @returns An Array of Tree
*/
function parse_dir( $folder ){
$dir = @opendir( $folder );
$fname = array_pop( explode( "/",$folder) );
$fname = empty( $fname ) ? "root" : str_replace( " ","_",$fname );
$path = "";
$filecount = 0;
$foldercount = 0;
$xml = "";
$tree = array();
$limb = array();
while ( false != ( $item = @readdir( $dir ) ) ) {
if( $item == "." || $item == ".." ) continue;
if( is_dir( "$folder/$item" ) ){
$tree[][$folder] = parse_dir( "$folder/$item");
$foldercount++;
$limb['folders'] = $foldercount;
$filecount++;
$limb['files'] = $filecount;
continue;
}
$limb['name'] = $item;
}
$tree[] = $limb;
return $tree;
}
}
$class = new DirTree();
/* view array */
echo "<pre>";
print_r($class->DirTree("../../flashservices"));
echo "</pre>";
?>
Warning: Missing argument 1 for DirTree::DirTree() in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 23 Notice: Undefined variable: dir_tree in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 26 Notice: Undefined offset: 0 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34 Notice: Undefined offset: 1 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34 Notice: Undefined offset: 2 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 0 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 1 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 2 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
<?php
class DirTree {
/**
* Get a tree of folders and files from a spec dir
* @returns An ArrayCollection of Tree
*/
function DirTree( $dir_tree ){
$t = array();
$_tree = $this->parse_dir($dir_tree);
for($a = 0; $a < count($_tree); $a++){ $t[] = new Tree($_tree[$a][0], $_tree[$a][1], $_tree[$a][2]); }
return $t;
}
//...
}
?>
This class constructor [DirTree( $dir_tree)] is being used to return an array that is built based on the value of $dir_tree. However, when you instantiate the DirTree object, you do it like this:
Since you have everything in the class wrapped around the output array from the constructor, and you have no error checking or exception handling, when you instantiate the object with a null parameter, all the other errors are raised.
<?
class Tree {
var $folders;
var $files;
var $name;
// explicit actionscript package
var $_explicitType = "tutorials.Tree";
function Tree($folders, $files, $name)
{
$this->folders = $folders;
$this->files = $files;
$this->name = $name;
}
}
class DirTree {
/**
* Get a tree of folders and files from a spec dir
* @returns An ArrayCollection of Tree
*/
function DirTree( $dir_tree ){
$t = array();
$_tree = $this->parse_dir($dir_tree);
for($a = 0; $a < count($_tree); $a++){ $t[] = new Tree($_tree[$a][0], $_tree[$a][1], $_tree[$a][2]); }
return $t;
}
/**
* Get a tree of folders and files from a spec dir
* @returns An Array of Tree
*/
function parse_dir( $folder ){
$dir = @opendir( $folder );
$fname = array_pop( explode( "/",$folder) );
$fname = empty( $fname ) ? "root" : str_replace( " ","_",$fname );
$path = "";
$filecount = 0;
$foldercount = 0;
$xml = "";
$tree = array();
$limb = array();
while ( false != ( $item = @readdir( $dir ) ) ) {
if( $item == "." || $item == ".." ) continue;
if( is_dir( "$folder/$item" ) ){
$tree[][$folder] = parse_dir( "$folder/$item");
$foldercount++;
$limb['folders'] = $foldercount;
$filecount++;
$limb['files'] = $filecount;
continue;
}
$limb['name'] = $item;
}
$tree[] = $limb;
return $tree;
}
}
$class = new DirTree();
/* view array */
echo "<pre>";
print_r($class->DirTree("../../flashservices"));
echo "</pre>";
?>
Warning: Missing argument 1 for DirTree::DirTree() in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 23 Notice: Undefined variable: dir_tree in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 26 Notice: Undefined offset: 0 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34 Notice: Undefined offset: 1 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34 Notice: Undefined offset: 2 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 0 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 1 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34
Notice: Undefined offset: 2 in C:\apache2triad\htdocs\flashservices_v1.9\services\folder_tree\dir_tree.php on line 34