Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/54146-newbie-to-classes-i-need-some-guru-help/
Share on other sites

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);

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.