Can I instantiate a PHP class inside another class?

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

    People saying that it is possible to 'create a class within a class' seem to mean that it is possible to create an object / instance within a class. I have not seen an example of an actual class definition within another class definition, which would look like:

    class myClass{
    
        class myNestedClass{
    
        }
    
    }
    
    /* THIS IS NOT ALLOWED AND SO IT WON'T WORK */
    

    Since the question was 'is it possible to create a class inside another class', I can now only assume that it is NOT possible.

提交回复
热议问题