webhoes Posted November 16, 2017 Share Posted November 16, 2017 Hello, A general questions. A recently made a tag cloud for my website. I looked at an old module for that and based on that logic created a function in my _func.php. It contains a fourth of the code and does the same. I am curious in wether there a great differences in doing something via a function or making a module for it. Specially the pro's and cons for each like speed, maintainability or what is best practice. Sanne Link to comment Share on other sites More sharing options...
heldercervantes Posted November 17, 2017 Share Posted November 17, 2017 I've been doing functions from the start, but more recently I've had a couple of instances where I go for a custom module. I can't really speak in terms of performance, haven't made any tests, but the module approach feels better when there's more than one or two functions, to organise everything more neatly. Example: I'm working on an online store built from scratch. In that project the server shoots emails all the time. On register, on purchase, on order status changes, etc. Each email has a template that can be edited in the CMS. It becomes complex having to handle multiple templates and usage scenarios, replacing variables, different results based on context, so turning it into a module made it much more organised. I guess for me it's about volume and reusability. If it's small stuff I don't bother. 2 Link to comment Share on other sites More sharing options...
bernhard Posted November 17, 2017 Share Posted November 17, 2017 i don't think there is any noticable performance difference (just guessing). the main difference imho is reusability. for small things functions are totally fine. there is a reason why the _func.php file exists in the profile but if you use your function across several pw installations it's for sure better to pack them in a module, host them on some version control system and push updates to all of your sites easily whenever you want. that could get tedious when you have your code spread all over your sites and _func files... 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 17, 2017 Share Posted November 17, 2017 I'd try to keep things decoupled. So if your php functionality does not depend on anything processwire I'd keep in in a separate class/file even if it's going to be used in a module, which does bind/hook the functionality into processwire. 2 Link to comment Share on other sites More sharing options...
webhoes Posted November 18, 2017 Author Share Posted November 18, 2017 Those are very clear answers. Thanks Currently all functions are specific to a site as they also contain small parts of the template. I do see stuff coming in the future that could be used in more sites. By then I should make it a module. Making a module would be the next challange to learn. Link to comment Share on other sites More sharing options...
bernhard Posted November 18, 2017 Share Posted November 18, 2017 1 hour ago, webhoes said: Making a module would be the next challange to learn. i encourage you to take this challenge as soon as possible. it's really not hard and it will make your head think differently in some situations and make your code cleaner and easier to maintain. and in the end that will save you time have fun 2 Link to comment Share on other sites More sharing options...
webhoes Posted November 18, 2017 Author Share Posted November 18, 2017 Challenge accepted 3 Link to comment Share on other sites More sharing options...
Recommended Posts