2

I have a PHP class with a __construct() method. I pass parameters and store them in properties. Like this:

class BlogList {

  private $app_config, $texts;

  public function __construct($app_config, $texts) {
      $this->$app_config = $app_config;
      $this->$texts = $texts;
  }

  ...

}

When I create an instance of this class I see that the logs say this:

PHP Notice:  Array to string conversion in /path/to/blogList.php on line 6

I get this for both assignments in the __construct method. Both $app_config and $texts are arrays

Why is this conversion done? And how can I prevent it?

2
  • 1
    Don't use variable variables when you assign the properties in the construct. $this->app_config Commented Dec 21, 2014 at 21:29
  • I read over that like 1000 times. I am a n00b. Thx Commented Dec 21, 2014 at 21:31

1 Answer 1

8

The assignments should be:

public function __construct($app_config, $texts) {
    $this->app_config = $app_config;
    $this->texts = $texts;
}
Sign up to request clarification or add additional context in comments.

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.