...

View Full Version : Newbie to Classes, I need some Guru help



cesarcesar
06-04-2007, 07:32 PM
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.

aedrin
06-04-2007, 07:37 PM
Try this:



$_tree = $this->parse_dir($dir_tree);

cesarcesar
06-04-2007, 08:05 PM
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>

mr e
06-04-2007, 08:20 PM
What errors?

aedrin
06-04-2007, 09:29 PM
var $folders;
var $files;
var $name;
// explicit actionscript package
var $_explicitType = "tutorials.Tree";


Just a minor suggestion. The correct way to define a class variable is with the keyword private, protected or public, not var.

Also, please use the PHP button at the top right to enclose your code. Just using code is harder to read.

kbluhm
06-04-2007, 09:41 PM
Just a minor suggestion. The correct way to define a class variable is with the keyword private, protected or public, not var.

That is assuming he is using PHP5. You'll notice he's using a PHP4 constructor. If he's going to use public, private, or protected to define his properties, then he might as well use function __construct( $dir_tree ) { ... } as the constructor.

If you're on PHP4, you're stuck using 'var'. If you use anything other than 'var' on a PHP install < 5, you'll get an error similar to the following:

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in...

aedrin
06-04-2007, 10:25 PM
Correct, but why troubleshoot a deprecated version?

mr e
06-04-2007, 10:27 PM
Correct, but why troubleshoot a deprecated version?
What if that's all his current host supports? Shouldn't we still help regardless?

aedrin
06-04-2007, 10:31 PM
If your host does not support PHP5, why would you be paying them?

Would you buy a computer from Dell if it had Windows ME on it?

Fumigator
06-04-2007, 11:04 PM
If it were half the cost of a computer with XP on it, then yeah I would.

aedrin
06-04-2007, 11:09 PM
You get what you pay for. ;)

Fumigator
06-04-2007, 11:12 PM
Pay $600 for a Dell with XP or $300 for a Dell with ME and $100 for an OEM XP license.... hmmm that's a toughie!

I forgot what we were arguing about.

firepages
06-05-2007, 02:26 AM
Correct, but why troubleshoot a deprecated version?
.. and why give an answer which might break someones code without warning them ?

If your host does not support PHP5, why would you be paying them?
there are probably still more PHP4 hosts than PHP5 hosts out there today, though the balance is tipping, why is a whole other thread.

kbluhm
06-05-2007, 04:08 AM
If your host does not support PHP5, why would you be paying them?

Would you buy a computer from Dell if it had Windows ME on it?

You're kidding, right? So in other words he's not deserving of help until he upgrades out of the dark ages of PHP 4? Hahahahaha, good one.

It's not always the fault of the user. The company I work for does a lot of work with Yahoo! Small Business. At this point their hosting still only supports version 4.3. You sometimes have to work with what you're given. Don't make him out to be pathetic, or unworthy of your spectacular advice, because he's still working with 4.

And if he is still on a 4 platform, your suggestion would have totally annihilated his code.

firepages
06-05-2007, 05:15 AM
Guilty myself ;) but can we please keep replies now in line with the original question.

@ Aedrin & Fumigator
like I said, on topic or not at all please, posts deleted.

cesarcesar
06-05-2007, 07:05 PM
I love how off topic this got!

Correct, but why troubleshoot a deprecated version?
I feel this is true. I really don't want to know how to do it right in php 4 when 5 is new and updated, and as far as my host goes.... PHP 5 been out for a year now. Hosts need to get with the picture. Realistically, how many daily PHP developers are not on their own co-lo or dedicated server anyways.


Pay $600 for a Dell with XP or $300 for a Dell with ME and $100 for an OEM XP license.... hmmm that's a toughie!
Very Smart Idea...

firepages
06-05-2007, 07:18 PM
how many daily PHP developers are not on their own co-lo or dedicated server anyways

thousands, some of us have dedicated/VPS & co-lo's but still have to work on shared hosts on a daily basis many without PHP5 since PHP4 is far from depracated.

there was nothing wrong with the original advice about private vars apart from omitting the `PHP5 only` point, which was fairly commented on, then it went downhill.

Since you yourself seem unconcerned with the original thread I will close it, if anyone wants to start a PHP4 vs PHP5 debate please feel free to do so.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum