How can I call a static method on a variable class?

前端 未结 6 1863
轻奢々
轻奢々 2020-12-15 15:44

I\'m trying to make some kind of function that loads and instantiates a class from a given variable. Something like this:



        
6条回答
  •  一整个雨季
    2020-12-15 16:06

    It looks like you are fighting PHP's current implementation static binding, which is why you are jumping through hoops with getCallingClass. I can tell you from experience, you should probably abandon trying to put instantiation in a parent class through a static method. It will cause you more problems in the end. PHP 5.3 will implement "late static binding" and should solve your problem, but that obviously doesn't help now.

    You are probably better off using the autoload functionality mentioned by kodisha combined with a solid Singleton implementation. I'm not sure if your goal is syntactic sugar or not, but it think you will do better in the long run to steer clear of trying to save a few characters.

提交回复
热议问题