dimanche 19 avril 2015

PHP design pattern for classes with static and non-static methods

My objective is to create class that can be used both static and non-static way. Both ways have to use the same methods, but in different way


Non-static way:



$color = new Color("#fff");
$darkenColor = $color->darken(0.1);


Static way:



$darkenColor = Color::darken("#fff", 0.1);


So in this example method darken can be used both on existing object and as static method of Color class. But depending on how it's used, it uses different parameters.


How should such class be designed? What is good pattern for creating such types of classes?


Class will have a lot of different methods so it should avoid massive checking code in beginning of each method.


ps. I wasnt sure what should be title of this quesiton. If you've got better idea so other people with same problem can find it - please edit.


Aucun commentaire:

Enregistrer un commentaire