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 8434779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:45:03+00:00 2026-06-10T06:45:03+00:00

I have an object that is responsible for exporting a file to csv. It

  • 0

I have an object that is responsible for exporting a file to csv.

It works well but i am looking at ways to refactor it.

This question pertains to the constructor which takes a number of arguments, relating to how the csv is to be exported:

For example, file name, delimiter, etc. etc.

Also, lately I have been reading about dependency injection but cant decide if this is a case where I should:

A. Leave the constructor as is.
B. Create a new class that gets passed to the constructor that simple holds config values for the file name etc
C. Something else altogether?

Here is the existing constructor (in PHP)

    public function __construct($file,$overwriteExistingFile, $enclosure, $delim, $headerRow)
    {
        //set all properties here
    }
  • 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-06-10T06:45:04+00:00Added an answer on June 10, 2026 at 6:45 am

    Each of those values represents data that is an input to some process. $enclosure, $delimiter, and $headerRow pertain to generating the CSV content, while $file and $overwriteExistingFile pertain to persisting the content to disk.

    A hallmark of a DI-style refactoring is to identify the various responsibilities (generate, persist) and encapsulate each of them in its own type. This shifts the refactoring from “how do I best get the values to this class?” to “how do I remove knowledge of these values from this class?”

    To answer that, we would define two new concepts, each of which takes one of the responsibilities, and pass those into the existing constructor:

    public function __construct($csvGenerator, $csvFileWriter)
    {
        ...save dependencies...
    }
    
    ...at some point, generate the CSV content and pass it to the file writer...
    

    In this way, the original class becomes the orchestrator of the interaction between the generation and file writing, without having intimate knowledge of either activity. We have elevated the class to a higher level of abstraction, simplifying it as well as isolating its responsibilities into its collaborators.

    Now, you would define two new classes, constructing them with the relevant parameters:

    Generator

    public function __construct($enclosure, $delimiter, $headerRow)
    

    File Writer

    public function __construct($file, $overwriteExistingFile)
    

    With these elements in place, you can compose them together by creating the generator, then the file writer, then passing both to the orchestrator.

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

Sidebar

Related Questions

This might be too opinionated a question, but looking for help! I have been
I have a service object that is responsible for some business logic validation. What
I have an object that looks like this { AF : { name :
I have an object that has properties, but when I try to access them,
I always thought I understood how this works...but lately I have started really using
I have a object that is responsible for controlling screen navigation. A presenter can
Inside a container object, I have created a class Factory , that is responsible
I have two tables: object that has object_id column and avalues that have object_id
I have an object that cannot be copied, a NetGame because it has a
I have an object that has pairs of replacement values used for simple encoding

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.