Can I instantiate a PHP class inside another class?

前端 未结 8 822
挽巷
挽巷 2020-12-08 03:57

I was wondering if it is allowed to create an instance of a class inside another class.

Or, do I have to create it outside and then pass it in through the constructo

8条回答
  •  自闭症患者
    2020-12-08 04:32

    I'm just seeing this now and I wish to add my comment. It could be of benefit to anyone. Has anyone heard of php traits? Although traits does not import another class but only imports a trait into another class. So instead of trying to import several classes, you can have a main classe while the subclasses will act like traits making it possible for you to use them in main/parent classes

提交回复
热议问题