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)) : 

		$str = (isset($this->Params['str'])) ? $this->Params['str'] : NULL;

		return $str;

	endif;

}

private function _makeArray() {

	if(is_array($this->Params)) :

		$str = (isset($this->Params['array'])) ? $this->Params['array'] : NULL;

		$Array = explode(',', $str);

		return $Array;

	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?

    laserlight;11006191 wrote:

    Why not use a namespace instead?

    Ok, so I've totally been ignoring namespaces... partly because I haven't had time and partly because I don't think I've read about any other feature in PHP with such a mixed reaction in terms of it's usefulness...

    So, you're gonna have to educate me a little bit - What would be the benefit of using a namespace here?

    There's no danger of collision... And wouldn't I need to then declare the namespace on any files that need to use the methods?

    Again, namespace newb here, lol. 😉

    Also, my primary reason for putting disparate but related functions into a class, aside from tidiness, is the ability to dynamically load them via spl_autoload_register().

      antmeeks wrote:

      What would be the benefit of using a namespace here?

      Well... you were rightly talking about how things should be simple in a certain other thread, yet it looks like all you've done here is complicate simple function calls by requiring otherwise unnecessary temporary object creation 😉

      antmeeks wrote:

      There's no danger of collision...

      Avoiding that is a major reason for the use of namespaces, but another reason is just to group related functions and such.

      antmeeks wrote:

      And wouldn't I need to then declare the namespace on any files that need to use this class?

      No, the namespace should be declared when you want the names defined in the file to belong to that namespace. What you could do is aliasing/importing of namespaces.

        laserlight;11006194 wrote:

        Well... you were rightly talking about how things should be simple in a certain other thread, yet it looks like all you've done here is complicate simple function calls by requiring otherwise unnecessary temporary object creation 😉

        You are absolutely 100% right. I'm busted.😃

        I suspected (knew) it was dumb, but I posted anyway just to be sure.

        Honestly it should just be a collection of static methods wrapped in a class declaration for convenience & tidiness.

        laserlight;11006194 wrote:

        Avoiding that is a major reason for the use of namespaces, but another reason is just to group related functions and such.

        It certainly is an interesting idea.

        laserlight;11006194 wrote:

        No, the namespace should be declared when you want the names defined in the file to belong to that namespace. What you could do is aliasing/importing of namespaces.

        I'm gonna have to do more reading on PHP's namespaces to educate myself. Thanks for the input!

          Write a Reply...