0

I have:

class account {
    private function something() {
#       TRYING TO PUT ARRAY HERE
    }
}

But, it keeps giving me an error when I use either of these. I googled how to create an array within a class, to no avail.

private array options = ['cost' => 20];

array options = ['cost' => 20];

How is it done? Thanks.

1

2 Answers 2

1

You seem to be missing some of the fundamentals of PHP. You need to refactor your code as follows:

class account
{
    private $options;

    private function something() 
    {
        $options = array();
        $options['cost'] = 20;

        $this->options = $options;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to create a property of a method which contains an array. You could declare it like this:

class Account
{
    private $options;

    public function setOptions()
    {
        $this->options = array('options' => 20);
    }

}

Or you could just set it like this:

class Account
{
    private $options = ['cost' => 20]; // set property

    public function getOptions()
    {
        return $this->options;
    }
}

$obj = new Account;
$options = $obj->getOptions();
print_r($options);

Comments

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.