Jump to content

[SOLVED] OOP organization


Boo-urns

Recommended Posts

I am thinking of having a few classes for different sections / events for a site. First off I have extended mysqli class. Now where I am confused is I have a class for validation (passwords, zipcodes...etc) Is it best to have a db connection there as well or is there a better way of doing that?

 

Thanks!

Link to comment
Share on other sites

One simple possible consideration you may have is to implement a database interaction object as a singleton and retrieve the object when it is needed. Unfortunately that also leads to a global connection that can be fairly limiting when implementing your other classes. What I prefer to do is instead of using a singleton or, as suggested before, creating a new database object inside of the class, is to use dependency injection and pass the class a data object when the class is instantiated (edit: or when specific methods are invoked). This allows for more flexible testing of the class and more flexible types of data sources without having to manipulate global data and further interfering with the tested object's encapsulation.

Link to comment
Share on other sites

One simple possible consideration you may have is to implement a database interaction object as a singleton and retrieve the object when it is needed. Unfortunately that also leads to a global connection that can be fairly limiting when implementing your other classes. What I prefer to do is instead of using a singleton or, as suggested before, creating a new database object inside of the class, is to use dependency injection and pass the class a data object when the class is instantiated (edit: or when specific methods are invoked). This allows for more flexible testing of the class and more flexible types of data sources without having to manipulate global data and further interfering with the tested object's encapsulation.

 

I am not sure of this in PHP5, but I had that setup in my scripts in PHP4 and it was amazing at how much slower the script ran by passing in the objects. I actually had a "Main" method where I would instantiate an object/return it if called, so only one instance of the object was created, but for whatever reason it slowed it down a ton. Since, this was for PHP4, I created functions, it would still only instantiate the class on a first call, but allowed for that to be used throughout any class, and it worked well.

 

As far as this on PHP5, I have no clue of the side-effects.

 

Just thought I would share my PHP4 experience with that scenario of including the class with the constructor or parameter.

Link to comment
Share on other sites

genericnumber1 - can you somewhat describe dependency injection?

 

I've found an example but I'm having problems creating it to work. I'm getting an error: "Fatal error: Call to undefined method BookReader::getChapers()" Which i imagine is the DI not working.

 

<?php
interface Db {
  public function executeSql($sql);
}

class MySqlDb implements Db {
  public function __construct($username, $password, $host) {
    // i took out the constructor and used the mysqli constructor
  }
  public function executeSql($sql) {
    // return something here nothing new...
  }
}

class BookReader {
  private $_db;
  public function __construct(Db $db) {
    $this->_db = $db;
  }
  public function getChapters() {
    return $this->_db->executeSql('SELECT name FROM chapter');
  }
}

$book = new BookReader(new MySqlDb(DB_USER, DB_PASS, DB_HOST));
$chapters = $book->getChapers();

 

Instead of using an interface i just extended the mysqli class for MySqlDb. I just can't wrap my head around this yet :-/

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.