Reputation: 52501
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:
Upvotes: 3
Views: 156
Reputation: 802
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.
Upvotes: 1
Reputation: 1822
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.
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.
Upvotes: 3
Reputation: 7472
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');
Upvotes: 0