Jump to content

Generic functionality Abstract or not?


Thierry

Recommended Posts

I have some generic functionality in my application that I'd like to put into classes, as currently all the functions are in a single php file which is then included.

 

However I'm not sure whether or not I should make the class abstract (or should I even use a class).

 

For example, my application comes across dates all the time and has to swap them between the ISO format of YYYY-MM-DD and the format of DD-MM-YYYY or MM-DD-YYYY.

Another function for dates checks if it's a valid one and yet another translates any given date to seconds.

 

I'd rather put such functionality under classes, but I was wondering how that should be done.

 

On one side I feel it should be abstract and made available everywhere, so that you can simple do a 'Date::Format($date)'.

Since there is no need to create multiple instances of the class, abstract would seem to do fine.

Still, this leaves me to wonder what the point would be of using a class only as abstract without extending it (aside from some encapsulation and class constants).

 

The other way would be to keep it not abstract and create an instance either once at the top or each time for each page and use '$Date->Format($date)'

 

This example is ofcourse just with dates, I have quite a few more for strings, numbers etc.

 

What should I do? Where does generic functionality that an application will use everywhere fit in OOP?

Link to comment
Share on other sites

abstract classes cannot be instantiated.

 

The design pattern you are looking for is the registry pattern - simply make all your methods static so they can be called without an instance of the class being created - although this will achieve the same as having a bunch of helper functions but may incur a little more overhead.

Link to comment
Share on other sites

abstract classes cannot be instantiated.

 

The design pattern you are looking for is the registry pattern - simply make all your methods static so they can be called without an instance of the class being created - although this will achieve the same as having a bunch of helper functions but may incur a little more overhead.

 

I'm aware that abstract classes can't be instantiated (that's what it's for), I was more curious as to whether or not to keep it that way when faced with generic functions/methods.

Looks like keeping it static/abstract and doing Date::Format is the way to go.

 

I suppose the other way would be to make an object out of literally everything so that a new date value becomes a Date object with $date->Format on it. Would make even the simplest operation a lot more complicated though, as I'd have to do stuff like $int->getVal() just to get the value of an int.

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.