Jump to content

Adding common methods to a class


Recommended Posts

I've written multiple generic methods which are not specific to any class or application.  Examples include initiating a http request, recursive searching an array, displaying a pdo query for troubleshooting, etc.  For some projects, I've tried to separate them in different classes based on what they do and have called them statically, and for other projects, I have just dumped them in a "Helper" class, and have had all other classes extend that class.  What should I be doing?

Link to comment
Share on other sites

If all of these disparate functions that you have written do not logically 'fit' together, then lumping them into a helper class unnecessarily bloats any script that you use it in.  I have many many modules that I have written that I include when needed as separate functions without trying to 'class-ify' them.

Link to comment
Share on other sites

How do you manage them?  Maybe something like the following?  I was also thinking there might be a good way to do so using composer.

 

    public function execFunction($function,array $arr)
    {
        if (!function_exists($function)) {
            require("/path/to/$function");
        }
        return $function($arr);
    }
Link to comment
Share on other sites

Recommend calling them statically if they only perform a single set functionality?

 

Yes.

 

 

 

No good reason to attempt to use composer to manage?

 

I don't see the point.

 

Since you already have an autoloader and are in an OO environment, putting the features into (static) methods is the obvious solution. Why create a parallel infrastructure?

Edited by Jacques1
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.