antmeeks Posted June 15, 2012 Share Posted June 15, 2012 So I've decided to put several disparate but related functions into helper classes. Here's an example of what such a class would look like (with obviously more involved methods), and I'd like a critique on it's structure. class MyHelpers { public $str; public $Array; private $Params; public function __construct($method, $Params) { $method = '_' . $method; $this->Params = (is_array($Params)) ? $Params : NULL; $result = (method_exists($this, $method)) ? $this->$method() : NULL; (is_array($result)) ? $this->Array = $result : $this->str = $result; } private function _outputString() { if(is_array($this->Params)) : return (isset($this->Params['str'])) ? $this->Params['str'] : NULL; endif; } private function _makeArray() { if(is_array($this->Params)) : $str = (isset($this->Params['array'])) ? $this->Params['array'] : NULL; return explode(',', $str); endif; } } And here's how you would instantiate and use it: $GetResult = new MyHelpers('outputString', array('str'=>'This is a string.')); $str = $GetResult->str; $GetResult = new MyHelpers('makeArray', array('array'=>'1,2,3')); $Array = $GetResult->Array; Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/264206-helper-class-definition-is-this-example-good-or-bad/ Share on other sites More sharing options...
requinix Posted June 15, 2012 Share Posted June 15, 2012 Anything wrong with just using simple functions? Quote Link to comment https://forums.phpfreaks.com/topic/264206-helper-class-definition-is-this-example-good-or-bad/#findComment-1354003 Share on other sites More sharing options...
antmeeks Posted June 15, 2012 Author Share Posted June 15, 2012 Lol, I knew that question would come up... I have two reasons for doing this: 1. To keep the functions organized in a meaningful way. 2. To load the files dynamically with spl_autoload_register(). Quote Link to comment https://forums.phpfreaks.com/topic/264206-helper-class-definition-is-this-example-good-or-bad/#findComment-1354005 Share on other sites More sharing options...
requinix Posted June 15, 2012 Share Posted June 15, 2012 #1 can be easily solved with sticking the functions in a file in a well-known place, but #2 is actually one of the few decent reasons. Use static methods, not instance methods. Make the class a mere container of functions, not an object of sorts. class MyHelpers { public static function makeArray($array) { return explode(',', $array); } public static function outputString($str) { return $str; } } Quote Link to comment https://forums.phpfreaks.com/topic/264206-helper-class-definition-is-this-example-good-or-bad/#findComment-1354012 Share on other sites More sharing options...
antmeeks Posted June 15, 2012 Author Share Posted June 15, 2012 #1 can be easily solved with sticking the functions in a file in a well-known place, but #2 is actually one of the few decent reasons. I did have them organized in a separate files, but wanted to eliminate as many manual includes/requires as possible. Use static methods, not instance methods. Make the class a mere container of functions, not an object of sorts. Sigh... Yes, you are probably right - that's the best approach... I guess I was just trying to be too clever... I suspected it was dumb, which is why I posted it, lol. Quote Link to comment https://forums.phpfreaks.com/topic/264206-helper-class-definition-is-this-example-good-or-bad/#findComment-1354015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.