SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Maryland, USA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OO array of Objects

    Okay, here is the quest. I am trying take a delimited text file and create an array of custom objects.


    I have made it this far:
    PHP Code:
    class dictionaryItem {
    var 
    term;
    var 
    part;
    var 
    definition;
    var 
    itemArr;
      function 
    setDictItem($itemArr){
        list(
    $this->term$this->part$this->definition) = explode("|",$itemarr);
      }
      function 
    toString() {
        echo 
    "<p>this item contains</p>";
        echo 
    $this->term "<br>" $this->part "<br>" $this->definition .   "<br>";
      }
      function 
    getTerm() {
        return 
    $this->term;
      }
      function 
    getPart() {
        return 
    $this->part;
      }
      function 
    getDefinition() {
        return 
    $this->definition;
      }
    }
    // end of class declaration
    $inputfile = array();
    $dictionary = array();
    $inputfile file('dictionary');
    for (
    $i 0$i<sizeof($inputfile);i++) {
    $dictionary[$i] = new dictionaryItem();
    $dictionary[$i]-> setDictItem($inputfile[$i]);
    $dictionary[$i]->toString();

    what I would like to do is move the for loop inside the object and then return words based on a function that takes an argument like:
    PHP Code:
    echo dictionary->getWord("apple"); 
    It would also let me do things like return the entire dictionary in sorted order etc...

    Thanks in advance.

    Mike

  2. #2
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class dictionary extends dictionaryItems{
    variable here

    function dictionary(&$file){
    your foreach loop;
    }

    function 
    getWord($word){

    return 
    the word;
    }

    function &
    getDictionary(){
    return 
    the whole array;
    }

    Maybe something like this?


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
  •