开发者

how to make functions global?

i'm trying to follow DRY and i've got some functions i have to reuse.

i put them all as static functions in a class and want to use them in another class.

what is the best way to m开发者_开发知识库ake them available to a class.

cause i can't extend the class, its already extended.

should/could i use composition?

what is best practice?

thanks!


If they are static then just call them from your second class.

First_Class::method();


Public class methods (vs instance methods, e.g. non-static) are always available from the global scope, so you could just call them statically where needed. But keep in mind that static methods are death to testability and hard couple using classes to the global scope and the used class. You want to avoid that, so it's better practise to get rid of the static methods in favor of instance methods and passing in the dependency/object instance through the constructor or a setter. Add an interface Type Hint if you want to make sure the passed instance has a certain set of methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜