php - where to store helper functions? -
i've got lot of functions create or copy web.
i wonder if should store them in file include script or should store each function static method in class.
eg. i've got getcurrentfolder() , isfilephp() function.
should stored in file or each in class:
folder::getcurrent() file::isphp();
how do?
i know kinda "as u want" question great advices/best practices
thanks.
you're right, highly subjective matter use mix of 2 options.
you have class (say helper) has __call()
(and/or __callstatic()
if you're using php 5.3+) magic methods, when non defined [static] method called it'll load respective helper file , execute helper function. keep in mind though including files decreases performance, believe benefit gain in terms of file organization far outweighs tiny performance hit.
a simple example:
class helper { function __callstatic($m, $args) { if (is_file('./helpers/' . $m . '.php')) { include_once('./helpers/' . $m . '.php'); return call_user_func_array($m, $args); } } } helper::isfilephp(/*...*/); // ./helpers/isfilephp.php helper::getcurrentfolder(/*...*/); // ./helpers/getcurrentfolder.php
you can further optimize snippet , have several types of helpers (folder, file) , on, adding __call[static]()
magic method each of classes , implementing logic in folder/file structure of helper files/functions.
Comments
Post a Comment