4

I am getting the following error while running the following PHP code

class tableData
{
    private $row1 = array("Kalle1", "address1", "postal code1", "[email protected]", "070111001", "08111001");
    private $row2 = array("kalle2", "address2", "postcode2", "[email protected]", "070111002", "08111002");
    private $row3 = array("kalle3", "address3", "postcode3", "[email protected]", "070111003", "08111003");
    private $row4 = array("kalle4", "address4", "postcode4", "[email protected]", "070111004", "08111004");
    private $rader = array(array($row1),array($row2),array($row3),array($row4));
}

The error is corresponding to the variable $rader:

Parse error: syntax error, unexpected '$row1' (T_VARIABLE), expecting ')' in C:\xampp\htdocs\test_k.php on line 15

Can anyone help me correct this error?

1
  • Line 15 is the variable $rader in the above code. Commented Apr 9, 2018 at 1:22

2 Answers 2

2

The PHP manual states:

This declaration [of class properties] may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

private $rader = array(array($row1),array($row2),array($row3),array($row4));

So your $row1 is not a constant value, it requires run time information, ie the value that the variable holds.

When PHP is compiling your class, at the point of property initialisation, it has no idea what value your $row1 variable holds.

You'll have to set that variable to empty or null (whatever suits), and either have a setter method which sets it and call that first, OR use a constructor if this is always going to be the same data.

Basically, you cannot set properties from values that would come later from the object at run time.

Sign up to request clarification or add additional context in comments.

Comments

1

Use a constructor to declare class variables that are populated with other variables.

class tableData
{
    private $row1 = array("Kalle1", "address1", "postal code1", "[email protected]", "070111001", "08111001");
    private $row2 = array("kalle2", "address2", "postcode2", "[email protected]", "070111002", "08111002");
    private $row3 = array("kalle3", "address3", "postcode3", "[email protected]", "070111003", "08111003");
    private $row4 = array("kalle4", "address4", "postcode4", "[email protected]", "070111004", "08111004");
    private $rader;

    public function __construct() {
        $this->rader = array(
            array($this->row1),
            array($this->row2),
            array($this->row3),
            array($this->row4)
        );
    }

}

This assumes you do intend to create a 3-level nested array.

1 Comment

You're missing $this-> in $row1 ...`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.