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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:51:04+00:00 2026-06-10T09:51:04+00:00

I’m new to OOP and thought I’d give Silex a try on a small

  • 0

I’m new to OOP and thought I’d give Silex a try on a small app I’m attempting. I’m looking for some advice as to whether my design falls in line with good object oriented principles.

I have a User object which is basically just a bunch of properties, getters and setters. I then have a UserService object which will contain the logic for authenticating users, getting a user from the database, setting or updating user information, etc. I also have a UserServiceProvder class which is there to provide an instance of the UserService class to the app (which seems to be the best way to create a reusable chunk of code in Silex).

The question I have now is this: I am using the Doctrine DBAL that ships with Silex and when I instantiate the UserService class, I’m tempted to pass in a reference to the Doctrine object and then hard code calls to that object into methods of the UserService class.

For instance, to return a User from the database by id, I might create a method called getUserById($id) and then hardcode a Doctrine prepared statement into that method to select that user from the database and then return a User object.

Would it be better for me to create a whole other service that is just a further abstraction of the Doctrine DBAL and pass that to UserService when I instantiate it? That way, I could hard code the Doctrine prepared statements into that class, leaving my UserService class more encapsulated and reusable in case I decide to move away from Doctrine in the future.

I guess what I’m having a hard time with is realizing if there is a such a thing as overkill in OOP. It seems to me like the second method is much more reusable, but is it necessary or wise?

  • 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-10T09:51:06+00:00Added an answer on June 10, 2026 at 9:51 am

    Moving the Database access to a separate class will bring you a couple of advantages. First of all, if you keep the database access apart from the rest of your logic you can replace the implementation of your database access more easy. If for a reason you want to drop the Doctrine DBAL you’ll be happy that all the code is just referencing some interface to a repository instead of directly querying a database.

    A second great advantage is that you can test your application logic in separation of your database access logic. If you inject a Repository for users inside your UserService you can Mock this in your tests and be sure they only fail if something is wrong with the actual application logic.

    A small example of what you could do

    The interface is convenient for reference throughout your codebase. No code references the implementation, only the interface. That way you can easily replace the implementation of the interface without touching all the places it is used:

    interface IUserRepository
    {
      /**
       * @return User
       */
      public function getUserById($userId); 
    }
    

    Of course you do need an implementation of said interface. This is what you inject into your UserService. This is what you one day might replace with another implementation of the interface:

    class DoctrineDBALUserRepository implements IUserRepository
    {
      /**
       * @return User
       */
      public function getUserById($userId)
      {
        //implementation specific for Doctrine DBAL
      }
    }
    

    The UserService only knows about the interface and can use it freely. To avoid having to inject the UserRepository in a lot of places in your code you could create a convenience build method. Notice the constructor that references the interface and the build method that injects an implementation of that interface:

    class UserService 
    {
      private $UserRepository;
    
      public static build()
      {
        return new UserService(new DoctrineDBALUserRepository());
      }
    
      public function __construct(IUserRepository $UserRepository)
      {
        $this->UserRepository = $UserRepository;
      }
    
      public function getUserById($userId)
      {
        if ($User = $this->UserRepository->getUserById($userId) {
          return $User;
        }
        throw new RuntimeException('O noes, we messed up');
    }
    

    With this in place you can write tests for the business logic (e.g. throw an exception if saving fails):

    public function UserServiceTest extends PHPUnit_Framework_TestCase
    {
      public function testGetUserById_whenRetrievingFails_shouldThrowAnException()
      {
        $RepositoryStub = $this->getMock('IUserRepository');
        $RepositoryStub->expects($this->any())->method('getUserById')->will($this->returnValue(false);
    
        $UserService = new UserService($RepositoryStub);
        $this->setExpectedException('RuntimeException');
        $UserService->getUserById(1);
      }
    }
    

    I can imagine you’re not familiar with the last bit of code if you’re not into unit-testing yet. I hope you are and if not urge you to read up on that as well 😀 I figured it was good for the completeness of the answer to include it no matter what.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.