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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:03:47+00:00 2026-06-17T20:03:47+00:00

I’ve created two very basic Interfaces named ReaderInterface and WriterInterface, but I have removed

  • 0

I’ve created two very basic Interfaces named ReaderInterface and WriterInterface, but I have removed WriterInterface from this example since it is unnecessary to illustrate my conundrum.

ReaderInterface.php

interface ReaderInterface
{
    public function read();
}

I have a concrete class called Datatable:

Datatable.php

class Datatable
{
    protected $cols;
    protected $rows;

    protected $reader;

    public function __construct()
    {
        $this->cols = array();
        $this->rows = array();

        $this->reader = null;
    }

    public function setReader(ReaderInterface $reader)
    {
        $this->reader = $reader;
    }

    public function load()
    {
        //Contents to follow below.
    }
}

I instantiate a datatable instance as follows:

$db = new PDO("mysql:host=localhost;port=3306;dbname=test", "user", "pass"); //Let's pretend this is a good connection.

$datatable = new Datatable();
$datatable->setReader(new DatatableReader($db));
$datatable->load();

My question is about implementing DatatableReader so that it can read from the database I pass in, and write to $this->cols and $this->rows in my Datatable object.

I see two approaches right off.

1. Dependency Injection

class DatatableReader implements ReaderInterface
{
    protected $db;
    protected $datatable;

    public function __construct(Datatable &$datatable, PDO &$db)
    {
        $this->datatable = $datatable;
        $this->db = $db;
    }

    public function read()
    {
        //Execute prepared statement which returns 5 records.
        //Get the columns, and place them into an array.

        foreach ($columns as $col) {
            $this->datatable->addColumn($col); //Add a column to the datatable.
        }
    }
}

Then, my Datatable::load() method would be implemented as:

public function load()
{
    if ($this->reader != null)
        $this->reader->read();
}

2. Weakly typed return from read().

class DatatableReader implements ReaderInterface
{
    protected $db;

    public function __construct(PDO &$db)
    {
        $this->db = $db;
    }

    public function read()
    {
        //Execute prepared statement which returns 5 records.
        //Get the columns, and place them into an array.
        return $columns;
    }
}

I would then call my load() method as follows:

public function load()
{
    if ($this->reader != null) {
        $retval = $this->reader->read();

        //Do stuff with the array of information returned from the reader.
    }
}

Questions

  1. Of these two options, which is the best design?
  2. Is there a third option that I’m overlooking?
  • 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-17T20:03:49+00:00Added an answer on June 17, 2026 at 8:03 pm

    I would go with option 2. With option 1 you produce a recursion: DatatableReader contains object Datatable and vice versa.

    The other bad thing with option 1 is that you abuse the read method to write to another object.
    And only the concrete implementation (DatatableReader) knows about that object.
    All objects that implements an interface should react in the same way.

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.