How do you declare a class property as an object?
I tried:
public $objectname = new $Object();
But it didn’t work. Additionally, why should you do it like that?
Isn’t it better to just instantiate that object and just use its members?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the PHP manual on class properties (emphasis mine):
Either create it inside the constructor (composition)
or inject it in the constructor (aggregation)
or use setter injection.
You want to favor aggregation over composition.