cesarcesar Posted June 4, 2007 Share Posted June 4, 2007 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 http://www.sephiroth.it/tutorials/flashPHP/flex_remoteobject/page003.php <? class Tree { var $folders; var $files; var $name; // explicit actionscript package var $_explicitType = "tutorials.Tree"; } 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 = parse_dir($dir_tree); for($a = 0; $a < count($_tree); $a++){ $tree = new Tree(); $tree->folders = $_tree[$a][0]; $tree->files = $_tree[$a][1]; $tree->name = $_tree[$a][2]; $t[] = $tree; } 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("path_to_folder"); // "../../example_folder" echo $class->DirTree(); ?> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/54146-newbie-to-classes-i-need-some-guru-help/ Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 function DirTree( $dir_tree ){ $t = array(); $_tree = $this->parse_dir($dir_tree); for($a = 0; $a < count($_tree); $a++){ $tree = new Tree(); $tree->folders = $_tree[$a][0]; $tree->files = $_tree[$a][1]; $tree->name = $_tree[$a][2]; $t[] = $tree; } return $t; } changed $_tree = parse_dir($dir_tree); to $_tree = $this->parse_dir($dir_tree); Quote Link to comment https://forums.phpfreaks.com/topic/54146-newbie-to-classes-i-need-some-guru-help/#findComment-267696 Share on other sites More sharing options...
cesarcesar Posted June 4, 2007 Author Share Posted June 4, 2007 i have made some updates from suggestions and still get errors. Here is new code. <? class Tree { var $folders; var $files; var $name; // explicit actionscript package var $_explicitType = "tutorials.Tree"; } 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++){ $tree = new Tree(); $tree->folders = $_tree[$a][0]; $tree->files = $_tree[$a][1]; $tree->name = $_tree[$a][2]; $t[] = $tree; } 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>"; ?> Here is an XML view of what the structure should be like. <folder name="root" folders="1" files="2"> <file>advancedsettings.php</file> <file>adodbAdapter.php</file> <folder name="adapters" folders="1" files="5"> <file>adodbAdapter.php</file> <file>arrayfAdapter.php</file> <file>arrayftAdapter.php</file> <file>fbsqlAdapter.php</file> <file>informixAdapter.php</file> <folder name="custom" folders="0" files="2"> <file>CachedExecutionAction.php</file> <file>CachedGateway.php</file> </folder> </folder> </folder> Quote Link to comment https://forums.phpfreaks.com/topic/54146-newbie-to-classes-i-need-some-guru-help/#findComment-267724 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 Would you mind posting the errors? Quote Link to comment https://forums.phpfreaks.com/topic/54146-newbie-to-classes-i-need-some-guru-help/#findComment-267753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.