Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 589823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:27:50+00:00 2026-05-13T15:27:50+00:00

To show an example what is this question about: I have currently a dilemma

  • 0

To show an example what is this question about:

I have currently a dilemma in PHP project I’m working on. I have in mind a method that will be used by multiple classes (UIs in this case – MVC model), but I’m not sure how to represent such methods in OO design. The first thing that came into my mind was to create a class with static functions that I’d call whenever I need them. However I’m not sure if it’s the right thing to do.

To be more precise, I want to work, for example, with time. So I’ll need several methods that handle time. I was thinking about creating a Time class where I’d be functions that check whether the time is in correct format etc.

Some might say that I shouldn’t use class for this at all, since in PHP I can still use procedural code. But I’m more interested in answer that would enlighten me how to approach such situations in OOP / OOD.

So the actual questions are: How to represent such methods? Is static function approach good enough or should I reconsider anything else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:27:51+00:00Added an answer on May 13, 2026 at 3:27 pm

    I would recommend creating a normal class the contains this behavior, and then let that class implement an interface extracted from the class’ members.

    Whenever you need to call those methods, you inject the interface (not the concrete class) into the consumer. This lets you vary the two independently of each other.

    This may sound like more work, but is simply the Strategy design pattern applied.

    This will also make it much easier to unit test the code, because the code is more loosely coupled.


    Here’s an example in C#.

    Interface:

    public interface ITimeMachine
    {
        IStopwatch CreateStopwatch();
    
        DateTimeOffset GetNow();
    }
    

    Production implementation:

    public class RealTimeMachine : ITimeMachine
    {
        #region ITimeMachine Members
    
        public IStopwatch CreateStopwatch()
        {
            return new StopwatchAdapter();
        }
    
        public DateTimeOffset GetNow()
        {
            return DateTimeOffset.Now;
        }
    
        #endregion
    }
    

    and here’s a consumer of the interface:

    public abstract class PerformanceRecordingSession : IDisposable
    {
        private readonly IStopwatch watch;
    
        protected PerformanceRecordingSession(ITimeMachine timeMachine)
        {
            if (timeMachine == null)
            {
                throw new ArgumentNullException("timeMachine");
            }        
    
            this.watch = timeMachine.CreateStopwatch();
            this.watch.Start();
        }
    
        public abstract void Record(long elapsedTicks);
    
        public virtual void StopRecording()
        {
            this.watch.Stop();
            this.Record(this.watch.ElapsedTicks);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about usability/design. I currently am using some JQuery to hide/show
How can I select the good method (I have in the example below show
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
This question is about a general technique in SQL, that I can't quite work
I am currently building a multiplayer system, and I have a design question about
I have a question about Javascript widgets. The widget I am working on simply
To show, for example.... USER@SID > I thought this was potentially helpful to a
I will show the problem with a example. There is some text in the
First of all, this isn't another question about storing images on DB vs file
This is related to the question about return type attributes and anonymous classes ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.