3

I'm trying to decide if it's appropriate to use a class for declaring a set of ~20 public functions:

if (!class_exists('example')) { 

    class example {

        # Declare ~20 functions (methods), all of
        # which are public. (There's nothing else
        # in the class.)

        public static function one() { /* ... */ }
        public static function two() { /* ... */ }
        # ...

    } # class 

}

making the methods available as:

example::one();
example::two();

What pros/cons are there to the above approach vs. simply doing this:

if (!defined('EXAMPLE_LOADED')) {

    define('EXAMPLE_LOADED', true);

    function example_one() { /* ... */ }
    function example_two() { /* ... */ }
    # ... 

}

making the functions available as:

example_one();
example_two();

EDIT - related performance tests:

ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 1
    If using a high enough version of PHP, you could also namespace them. – Peter Ajtai Dec 03 '11 at 23:46
  • 3
    You should most likely not use classes for this. Classes should not be used as "container" for arbitrary functions. – Felix Kling Dec 03 '11 at 23:47
  • 1
    You might be better served with [namespaces](http://us.php.net/namespaces) - it depends on what version of PHP you're using. – Tim G Dec 03 '11 at 23:47
  • @TimG/Peter/Felix - Cool thanks - I'm reading about the namespaces now . I guess they're 5.3+ only. So if I namespaced it `example` then the functions would be available as `example::one` etc. right? – ryanve Dec 03 '11 at 23:55
  • read the manual. :) [basics](http://us.php.net/manual/en/language.namespaces.basics.php) – Tim G Dec 04 '11 at 00:14
  • @TimG This is going to be part of a WordPress theme and/or plugin, so I'm not going to be in control of the PHP version but I could test for it first. If I use the `namespace` then I still need a fallback at least down to 5.2. – ryanve Dec 04 '11 at 00:20
  • 1
    I would not use namespaces for a WP plugin just yet - I think 5.3 is still too new for that even though it's still over 2 years old. WordPress' [requirements](http://wordpress.org/about/requirements/) state PHP Version 5.2.4 or greater and I'd stick to that. – Tim G Dec 04 '11 at 03:07
  • @TimG Thanks, yea, but I'll definitely consider them for other stuff though. I've been digging through [FuelPHP](http://fuelphp.com), which uses namespaces all over the place to see what they've done with them. – ryanve Dec 06 '11 at 23:14

3 Answers3

3

Here is a link about this subject.

When to use static vs instantiated classes

I will also say that I've grouped similar functions into pure static classes before. I like the syntactical sugar it gives.

format::usaPhone( $val );
format::usaZip( $val );
date::convertTz( $date, $tz1, $tz2 );

Stuff like that. I would suggest you be pragmatic about this and make sure that what you do end up doing satisfies two things.

  1. You are happy with the result.
  2. The resulting code makes sense.

That may sound like a statement of the obvious but it's a good reminder to not over-think things when a simple solution presents itself.

Community
  • 1
  • 1
Tim G
  • 1,812
  • 12
  • 25
  • My main concern is making stuff that's easy for people to figure out how to use but at the same time keeping it as performance-optimized and maintainable as possible. I added links to some performance tests in the question, albeit the difference only matters when you do tons of operations. It does make sense to organize related funcs into static utility classes like that. I think that's easy for people to understand provided they're well-named. The namespace backslash is a trip if you've never seen it, but I think maintaining namespaced funcs would be easier b/c you don't need to use `self::` – ryanve Dec 06 '11 at 23:21
  • don't overlook the lowly function. :) You can namespace with naming conventions if you desire. – Tim G Dec 06 '11 at 23:23
  • I've been meaning to ask, why the if's, why the defines? Are you coming from a C background? C++,C#,C. Just use require_once for your libraries and forget about checking for already defined-ness. – Tim G Dec 06 '11 at 23:24
  • Yea straight up functions are definitely the fastest and definitely what people are most familiar with. – ryanve Dec 06 '11 at 23:26
  • I wanted to make it impossible to load twice, even it `require_once` wasn't used or say if it was loaded in two separate plugins in two separate files. No C background. – ryanve Dec 06 '11 at 23:27
1

well the difference between an oop and functional program approach is the overhead.

Definitely oop are good they could group your codes, easier to manage and the main feature it has that makes it ideal is that your codes will become reusable making it even more flexible that is why this approach is more ideal for large projects or systems. The downside of oop is it's more expensive in means of overheads and would be even slower if compared to functional approach though functional approach is much faster in performance it will not be as reusable as what you could expect with oop does but it'll all depend on you.

my advice to you is if it's really a large project your handling then oop is very good, but if you think it's not that necessary at all to do it in oop then go for functional or if possible you could also do the procedural way which has a faster performance than the 2 but as I said it all depends on your strategy.

Christopher Pelayo
  • 792
  • 11
  • 30
0

First case increase abstract level and help you to group (usually) helper methods.

Best practice is move your example class in separate file and then make when you need them

require_once('./path/to/example.class.php');
ruX
  • 7,224
  • 3
  • 39
  • 33