Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Newbie to Classes, I need some Guru help

    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/fl...ct/page003.php
    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 = 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.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Try this:

    PHP Code:
    $_tree $this->parse_dir($dir_tree); 

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts
    i have made some updates from suggestions and still get errors. Here is new code.
    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.
    Code:
    <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>

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    What errors?

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
        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.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by aedrin View Post
    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:
    Code:
    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in...
    Last edited by kbluhm; 06-04-2007 at 08:46 PM.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Correct, but why troubleshoot a deprecated version?

  • #8
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by aedrin View Post
    Correct, but why troubleshoot a deprecated version?
    What if that's all his current host supports? Shouldn't we still help regardless?

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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?

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If it were half the cost of a computer with XP on it, then yeah I would.

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You get what you pay for.

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #13
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,078
    Thanks
    11
    Thanked 98 Times in 96 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by aedrin View Post
    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.
    Last edited by kbluhm; 06-05-2007 at 03:22 AM.

  • #15
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,078
    Thanks
    11
    Thanked 98 Times in 96 Posts
    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.
    Last edited by firepages; 06-05-2007 at 04:53 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •