Page 1 of 2
Newbie to Classes, I need some Guru help
Posted: Mon Jun 04, 2007 12:25 pm
by cesarcesar
feyd | Please use Code: Select all
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]
Code: Select all
<?
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.
feyd | Please use Code: Select all
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]
Posted: Mon Jun 04, 2007 12:30 pm
by TheMoose
That's because parse_dir by itself, is undefined. You need to specify inside the class where to find it, by using $this->parse_dir()
Posted: Mon Jun 04, 2007 12:32 pm
by blackbeard
I believe you need to change this line:
$_tree = parse_dir($dir_tree);
To this:
$_tree = $this->parse_dir($dir_tree);
edit: Moose beat me to it.
Posted: Mon Jun 04, 2007 1:05 pm
by cesarcesar
feyd | Please use Code: Select all
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.
Code: Select all
<?
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.
Code: Select all
<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>
feyd | Please use Code: Select all
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]
Posted: Mon Jun 04, 2007 1:06 pm
by RobertGonzalez
What errors are you getting now?
Posted: Mon Jun 04, 2007 1:14 pm
by cesarcesar
feyd | Please use Code: Select all
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,
Code: Select all
<?
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
feyd | Please use Code: Select all
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]
Posted: Mon Jun 04, 2007 1:21 pm
by RobertGonzalez
The DirTree method requires a parameter that you are not passing when you instantiate the object.
Posted: Mon Jun 04, 2007 1:31 pm
by cesarcesar
yes, trying to work them out. not getting far.
Posted: Mon Jun 04, 2007 1:36 pm
by RobertGonzalez
Just so we are clear:
Code: Select all
<?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.
Posted: Mon Jun 04, 2007 1:42 pm
by cesarcesar
ok i understand a little. i am trying to get my head wrapped around classes.
Am i not initiating DirTree() correctly? Can post some links to where i can find info on where im going wrong.
Posted: Mon Jun 04, 2007 1:45 pm
by RobertGonzalez
Just out of curiosity, try this:
Code: Select all
<?php
$class = new DirTree('./');
?>
And see if that doesn't eliminate the error messages at this point. I just want to see if that works.
Posted: Mon Jun 04, 2007 1:53 pm
by cesarcesar
new code,
Code: Select all
<?
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
Posted: Mon Jun 04, 2007 2:01 pm
by RobertGonzalez
You're still doing the same thing as the code and example implementation before.

Posted: Mon Jun 04, 2007 3:00 pm
by TheMoose
Like Everah said, your class object has an initial parameter of $dir_tree, that you're not passing when you first create the object.
Code: Select all
$class = new DirTree("../../flashservices");
echo "<pre>";
print_r($class);
echo "</pre>";
Posted: Mon Jun 04, 2007 3:16 pm
by cesarcesar
Mr The Moose, thanks but is still no progress.