1

I wanted to initialize a class array as empty. For some reason, it's giving me the error "unexpected T_VARIABLE." Does anyone have an idea what's wrong with this class variable/array? This is what the class looks like:

class SentenceContentContainer {
    var $strSentence; //theSentence entered
    $arrayOfWords = []; //running the code has issue with this line

    function SentenceContentContainer($strSentence)
    {
        $this->strSentence = $strSentence;
    }

    function addWordToContainer(&$wordToAdd)
    {
        ...
    }
} //SentenceContentContainer
A.L
  • 10,259
  • 10
  • 67
  • 98
Michele
  • 3,617
  • 12
  • 47
  • 81
  • 4
    You need the `var` (or `public`, `private`, `protected`) keyword before `$arrayOfWords`. – gen_Eric May 04 '16 at 20:35
  • 1
    In a class context, the `$arrayOfWords` variable needs to be declared with visibility keywords. See http://php.net/manual/en/language.oop5.visibility.php – WillardSolutions May 04 '16 at 20:37

3 Answers3

3

Your variable is not defined correctly

class SentenceContentContainer {
    public $strSentence; //theSentence entered
    public $arrayOfWords = [] // running the code has issue with this line
    ....
}

choose public or private or protected, but var is less explicit, i prefer the others but its your choices. But your class variables must have a visibility keyword.

EDIT: as @AbraCadaver mention in this comment, the official documentation advise you to avoid var keyword

http://php.net/manual/en/language.oop5.visibility.php

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning

olibiaz
  • 2,551
  • 4
  • 29
  • 31
2

The code you're using is old PHP 4 syntax for objects, so stop using whatever resource you're learning from and start looking for something recent.

The var keyword and old-style constructors (function with same name as class) are both relics of a bygone age. You should be using the public keyword — assuming you need to access these variables publicly — and __construct() as a constructor function.

class SentenceContentContainer {
    public $strSentence; //theSentence entered
    public $arrayOfWords = []; //running the code has issue with this line

    function __construct($strSentence)
    {
        $this->strSentence = $strSentence;
    }

    function addWordToContainer(&$wordToAdd)
    {
        ...
    }
} //SentenceContentContainer

Note that unless you need to do something like this:

$sent = new SentenceContentContainer("Test sentence");
echo $sent->strSentence;

You should probably declare the variables as private instead of public.

miken32
  • 42,008
  • 16
  • 111
  • 154
0

Here you go:

    <?php

        class SentenceContentContainer {
            protected $strSentence;       //theSentence entered
            protected $arrayOfWords = []; //running the code has issue with this line

            function SentenceContentContainer($strSentence)
            {
                $this->strSentence = $strSentence;
            }

            function addWordToContainer(&$wordToAdd)
            {
                //...
            }
        } //Sentence
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • 1
    Posting a block of code with no explanation of what you've changed, or why, will not be helpful to a novice programmer. Try to fix the problem *and* teach a lesson at the same time! – miken32 May 05 '16 at 14:16
  • If i may apologise... please excuse me... i would take your words as an advice. Again... excuse me... the intention was never negative but i agree that if you are learning, you need explanations than mere blocks of code. ;-) – Poiz May 05 '16 at 14:41