SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Referential madness in document management system

    Intro
    I really must admit that I understand every example code I see in the tutorials on references, but using it myself is hard every time again, thus I have been avoiding them as much as possible.
    Now I have found that it is time to use them right, in my new project: a document management system.

    My problem
    I have a class called 'Folder' which has a name and filepath (location). All folders have a parent, except the root folder (where parent is null).
    The idea is to make a class 'Document' later, which holds a reference to a Folder, in order to locate it. If I want to move the document, I only have to change the reference.
    I am a bit in the dark on how to organise and traverse these directories (do they need a coordinating class maybe?).

    My current implementation is small, very small:

    PHP Code:
    <?php
    /**
    * ONADMS - Oh, Not Another Document Management System
    *
    * @author Y. Janse
    * @version $Revision$
    * @copyright 2004
    * @package ONADMS
    * $Id$
    */
    /**
    * Folder class.<br/>
    * Folder container object in FileSystem
    * @package ONADMS
    */
    class Folder {
      var 
    $parent;
      var 
    $name;
      var 
    $location;
      
      function 
    Folder(&$parent null$name$location)
      {
        
    $this->parent =& $parent;
        
    $this->name $name;
        
    $this->location $location;
      }
      
      function 
    getName()
      {
        return 
    $this->name;
      }
      
      function & 
    getParent() 
      {
        if(
    $this->parent != null) {
          return 
    $this->parent;
        } else {
          return 
    false;
        }
      }
      
      function 
    setParent(&$parent
      {
        
    $this->parent &= $parent;
      }
      
      function 
    printInfo()
      {
        echo 
    'I am ' $this->name '<br/>';
        echo 
    'I live in ' $location;
        if(
    $this->parent == null) {
          echo 
    ' by myself.<br/>';
        } else {
          echo 
    ' with my mommy ' $this->parent->getName() . '<br/>';
        }
      }
    }
    ?>
    I don't want to keep the printCode there, it will go to another class later, but is there for testing now.

    I test this class using the following:

    PHP Code:
    <?php
    require_once('lib/FileSystem/class.Folder.php');

    $f1 =& new Folder(null'Documents''/documents');
    $f2 =& new Folder($f1'Reports''/documents/reports');
    $f3 =& new Folder($f2'Finance''/document/finance');

    $f1->printInfo();
    $f2->printInfo();
    $f3->printInfo();
    ?>
    Now I thought I was doing it right, but Xdebug disagrees:
    Code:
    Parse error: parse error, unexpected '=', expecting ')' in c:\inetpub\wwwroot\onadms\lib\FileSystem\class.Folder.php on line 21
    Call Stack
    #	Function	Location
    1	{main}()	c:\inetpub\wwwroot\onadms\ignore.php:0
    
    Fatal error: Cannot instantiate non-existent class: folder in c:\inetpub\wwwroot\onadms\ignore.php on line 4
    Call Stack
    #	Function	Location
    1	{main}()	c:\inetpub\wwwroot\onadms\ignore.php:0
    Who can help me with this problem, and perhaps advise me on how to organise the Folders, maybe in a container of some kind?

    Thanks a heap in advance!
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |

  2. #2
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I remove the = null in the constructor of Folder I get another error:

    Code:
    Fatal error: Cannot pass parameter 1 by reference in c:\inetpub\wwwroot\onadms\ignore.php on line 4
    Call Stack
    #	Function	Location
    1	{main}()	c:\inetpub\wwwroot\onadms\ignore.php:0
    I can't reference null it seems, which is like a black hole?
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    NULL is always a black hole, nothing equals NULL, nothing is not equal to NULL, or greater than or less than NULL. NULL causes probelms all over the place in databases and SQL often handles it in strange ways. This is something they teach in DB classes at an early stage as it drives people mad at times.

    NULL is NOT a real value in a database.

    You have to use functions that detect "is null"

    Guess what the magic command is called in PHP?

    Yup, is_null()

    (and yes, I guessed that, then looked in the manual for confirmation)
    It's a boolean, sotrue or false gets returned.

    if (is_null($something) )
    {
    do this
    }
    else
    {
    do that
    }

    Never, EVER use if ($something == null)
    or if ($something !== null)
    They just don't work.

    According to the manual, there is a bit more to know about NULL as well, but I didn't read that bit.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yjanse

    There is no way to reference null correctly. Possible workaround: introduce special 'Null' singleton, which responds 'yes' on 'isNull' method, while all other objects should respond 'no'. Write a factory for this singleton and pass it whenever you need to pass 'null':

    PHP Code:
    // root object

    class TObject {
        function 
    isNull() {
            return 
    FALSE;
        }
    }

    // null singleton

    class TNull extends TObject {
        function 
    isNull() {
            return 
    TRUE;
        }
    }

    // work object

    class TNode extends TObject {
        var 
    $parent;
        var 
    $name;
        function 
    TNode(&$parent$name){
            
    $this->parent = &$parent;
            
    $this->name $name;
        }
    }

    // null factory

    function &getNull() {
        static 
    $_null 0;
        if(!
    $_null)
            
    $_null = new TNull();
        return 
    $_null;
    }

    // testing...

    $a = &new TNode(getNull(), 'a');
    $b = &new TNode($a'child of a');
    $c = &new TNode($b'child of b');

    $start = &$c;

    while(!
    $start->isNull()){
        echo 
    $start->name,"<br>";
        
    $start = &$start->parent;


  5. #5
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies guys, although it is not exactly what I meant.
    Your example stereofrog is very very helpful indeed for another problem I had, thanks!

    What my main problem was is not the passing of null as a reference, that was how I thought I could give a default parameter value to the $parent parameter so that I could create a root-node sort of item which had no parent, an orphan so to speak.
    What I really wanted to know is wether or not the &-ampersands are well placed in order to get the desired result.
    Or is my design in this case too wrong?

    I am sorry if I didnt make myself clear enough, I appreciate your efforts
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |

  6. #6
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shameless *bump*

    Does anyone know how I can make these references work so that each Folder references either another Folder, or nothing?
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |


Bookmarks

Posting Permissions

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