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

  • SEARCH
  • Home
  • 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 1094083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:55:20+00:00 2026-05-16T23:55:20+00:00

Sometimes I come across this problem where you have a set of functions that

  • 0

Sometimes I come across this problem where you have a set of functions that obviously belong to the same group. Those functions are needed at several places, and often together.

To give a specific example: consider the filemtime, fileatime and filectime functions. They all provide a similar functionality. If you are building something like a filemanager, you’ll probably need to call them one after another to get the info you need. This is the moment that you get thinking about a wrapper. PHP already provides stat, but suppose we don’t have that function.

I looked at the php sourcecode to find out how they solved this particular problem, but I can’t really find out what’s going on.

Obviously, if you have a naive implementation of such a grouping function, say filetimes, would like this:

function filetimes($file) {
    return array(
        'filectime' => filectime($file)
        ,'fileatime' => fileatime($file)
        ,'filemtime' => filemtime($file)
    );
}

This would work, but incurs overhead since you would have to open a file pointer for each function call. (I don’t know if it’s necessary to open a file pointer, but let’s assume that for the sake of the example).

Another approach would be to duplicate the code of the fileXtime functions and let them share a file pointer, but this obviously introduces code duplication, which is probably worse than the overhead introduced in the first example.

The third, and probably best, solution I came up with is to add an optional second parameter to the fileXtime functions to supply a filepointer.
The filetimes functions would then look like this:

function filetimes($file) {
    $fp = fopen($file, 'r');
    return array(
        'filectime' => filectime($file, $fp)
        ,'fileatime' => fileatime($file, $fp)
        ,'filemtime' => filemtime($file, $fp)
    );
}

Somehow this still feels ‘wrong’. There’s this extra parameter that is only used in some very specific conditions.

So basically the question is: what is best practice in situations like these?


Edit:
I’m aware that this is a typical situation where OOP comes into play. But first off: not everything needs to be a class. I always use an object oriented approach, but I also always have some functions in the global space.
Let’s say we’re talking about a legacy system here (with these ‘non-oop’ parts) and there are lots of dependencies on the fileXtime functions.

tdammer‘s answer is good for the specific example I gave, but does it extend to the broader problem set? Can a solution be defined such that it is applicable to most other problems in this domain?

  • 1 1 Answer
  • 3 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-16T23:55:21+00:00Added an answer on May 16, 2026 at 11:55 pm

    I’d rewrite the fileXtime functions to accept either a filename or a file handle as their only parameter. Languages that can overload functions (like C++, C# etc) can use this feature; in PHP, you’d have to check for the type of the argument at run time.

    Passing both a filename and a file handle would be redundant, and ambiguous calls could be made:

    $fp = fopen('foo', 'r');
    $times = file_times('bar', $fp);
    

    Of course, if you want to go OOP, you’d just wrap them all in a FileInfo class, and store a (lazy-loaded?) private file handle there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I have come across this issue that whenever I change the order of
An annoyance that I sometimes come across with SVN is the working copy getting
I am new to VB programming and have come across an problem:( after a
While looking at online code samples, I have sometimes come across an assignment of
I come across this problem when i am writing an event handler in SharePoint.
I've come across this twice now. Sometimes using the following line of code: [self.navigationController
In every webapp I build, I come across this issue. I want to set
Sometimes, I come across a property that, when I try to rename it using
Sometimes, I come across certain web development frameworks which don't provide an authentication feature,
Possible Duplicate: Why catch and rethrow Exception in C#? I sometimes come across C#

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.