#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Problems with arrays from book


    having a problem with simple script I took from a book on OOP. This is the second problem I've had with empty arrays.
    PHP Code:
    <?php
     
    class WordCounter
     
    {

         const 
    ASC 1;
         const 
    DESC=2;
         private 
    $words;
         function 
    __consruct($filename)
         {

         
    $file_content file_get_contents($filename);
         
    $this->words =(array_count_values(str_word_count(strtolower($file_content),1)));

         }

        public function 
    count($order)
        {

        if (
    $order==self::ASC)
        
    asort($this->words);
        else if(
    $order==self::DESC)
        
    arsort($this->words);
        foreach (
    $this->words as $key => $val)
        echo 
    $key ."="$val."<br/>";

        }

     }
    ?>
    call it on the index page
    PHP Code:
    <?php
    include("class_lib/class_wordcounter.php");

    include_once(
    "class_lib/words.txt");
     
    $wc = new WordCounter("words.txt");
    $wc->count(WordCounter::DESC);

    ?>
    and get this error
    PHP Code:
    Warningarsort() expects parameter 1 to be array, null given in C:\wamp\www\test\class_lib\class_wordcounter.php on line 22

    Warning
    Invalid argument supplied for foreach() in C:\wamp\www\test\class_lib\class_wordcounter.php on line 23 
    words.txt is a paragraph of words to be sorted and counted.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    it should be
    Code:
    __construct
    with a "t".

    By that, what is that
    PHP Code:
    include_once("class_lib/words.txt"); 
    supposed to do?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by Thanks,, tired eyes I guess

    By that, what is that
    [PHP
    include_once("class_lib/words.txt");[/PHP]
    supposed to do?
    this was just something I tried to make sure it was reading my file. I took it out and everything is good. Thanks

IMN logo majestic logo threadwatch logo seochat tools logo