0

I'm having difficulty initialising an associative array in PHP with custom object values. For example:

public $basket = [
    A => new Product("d", 2, 2)
]; // incorrect variant, interpretation error

public $basket = [
    "A" => "B", "C" => "d"
]; // correct variant

How to resolve this problem?

6
  • 2
    provide more info what you want to do Commented Dec 11, 2016 at 20:35
  • @piotr, I want init the associable array with values, which are the custom objects, as a "Product", how in my examples above, if it's possible Commented Dec 11, 2016 at 20:38
  • 1
    no idea what you are asking Commented Dec 11, 2016 at 20:46
  • 2
    @pragmus if the array is a class property, you can't initialize an object at the point where you declare it. You'll need to assign it somewhere, e.g. in your class's __construct method Commented Dec 11, 2016 at 20:49
  • (associable array looks weird enough to fit perfectly into PHP - I'm afraid it should read associative.) Commented Dec 11, 2016 at 23:47

1 Answer 1

1

I've initialized the object, in construct method, as Robbie Averill adviced and problem was resolved.

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.