How do I make functions global?

I am trying to follow DRY and I have some functionality that I have to reuse.

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

What is the best way to make them available to the class.

I cannot extend the class, its already extended.

should / could i use composition?

What's the best practice?

thanks!

+2


a source to share


2 answers


The public methods of the class (instance methods, for example, non-static) are always accessible from the global scope, so you can simply call them statically when needed. But keep in mind that static methods are death to check and a hard couple using classes for global coverage and class used . You want to avoid this, so it's better to get rid of static methods in favor of instance methods and passing the dependency / object instance through a constructor or setter. Add interface Enter a hint if you want to make sure that the passed instance has a specific set of methods.



+3


a source


If they are static, just call them from the second class.



First_Class::method();

      

+4


a source







All Articles