0

no matter where i search, i never found any rule that said class's property can't be an Array, but no matter how i tried, it never worked. It is against the rule to assign ARRAY as class's property? If so, is there any workaround? Here is my code

class Imperials{
protected $Data;

function __Construct($passedData){
       $this->$Data = $passedData;
       echo($this->$Data['Name']);
    }
}

$var = new Imperials(array('Name'='Buster','Race'='Nords'));

It would returned an error message

Fatal error: Uncaught Error: Cannot access empty property

4
  • 1
    first of all, you have mistake here $var = new Imperials(array('Name'='Buster','Race'='Nords')); shoud be key => value Commented Feb 16, 2018 at 16:21
  • You have a syntax error : 'Name'='Buster' should be 'Name'=>'Buster' same for 'Race'=>'Nords' Commented Feb 16, 2018 at 16:21
  • Read about PHP classes and arrays. Commented Feb 16, 2018 at 16:25
  • please, do read about PHP language syntax and grammar before posting here... Commented Feb 16, 2018 at 18:16

1 Answer 1

10

Use $this->Data without the $ instead of $this->$Data and use => for the array.

class Imperials{
    protected $Data;

    function __Construct($passedData){
        $this->Data = $passedData;
        echo($this->Data['Name']);
    }
}

$var = new Imperials(array('Name'=>'Buster','Race'=>'Nords'));
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh yes, i there they are, the $ on property name! as for the '=' instrad of '=>', it simply my mistake while reproduce the code here

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.