Can I instantiate a PHP class inside another class?

前端 未结 8 823
挽巷
挽巷 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:31

    No, it is not possible. Nesting classes and aggregating objects are diffrent mechanisms. Nesting classes means nesting class names, in fact. Class A nested in class B would be named "B\A" (?). From v5.3 you can nest class names (and some other names) in namespaces.

提交回复
热议问题