tags:

views:

87

answers:

3

I have a CORE class that pertains only to my specific site, ie, it performs site specific functions. I have a database class (for mysql), and other classes like access, validator, upload, template etc etc... I know that php classes can only extend one class each, so almost all of my classes extend the database class. I was looking over a public twitter class used on the twitter API. In it, there are functions to do almost everything you could do by going directly to the website, insert, delete, whatever... Should I put all site relatie functions inside my Core class and keep it out of the general scope of my scripts. Right now I have something like this....

Heres an example, as of right now I have functions like

$core = new Core();
$core->get_user_info($user_id);
$core->get_user_articles($user_id);

Inside that function, i perform database queries to select the needed information so i dont have to do it directly since it can get messy.

I also have functions to delete things, like

$core->Delete_Article($article_id);

However, I dont have functions to insert. Instead I use the Database class directly to add information, like so.

$article = array(user_id => $_SESSION['user_id'], body => $_POST['body']);
$db = new Database();
$db->Insert($article, 'articles');

or

$user = array(name => $_POST['name'], email => $_POST['email']);
$db->Insert($user, 'users');

Now, in the topic of separation of one aspect from another, should I put ALL of my database select/insert/update/delete queries inside my general CORE class and do ALL the database actions in the background

Like instead of $db->Insert(), i could use $core->insert_user() or just continue as I'm doing.

A: 

Because PHP is reloading everything on each page view, I do not see much advantage to instanciating classes for everything. If you break the functionality up into files that are roughly coorspond to your database schema, you can scale up and up without adding more and more functions that are loaded on each page hit.

It matters when you have dozens or hundreds of tables / classes.

Specifically for PHP, we prefer this style:

class Member
{
    public static function Insert($aData)
    {  
        //Insert member and return ID
    }
    public static function Select($Member_ID)
    {  
        //select member and return Array
    }

    public static function List($aFilter)
    {  
        //Return list of members filtered by specific criteria
    }
}


To take it one step further, we have a static class App which holds relevant "singletonish" variables.

class App
{
    public static $DB = NULL;
}

App::$DB = new Connection();

Now, anywhere in your app, you can say:

App::$DB->Query(...);
gahooa
A: 

Generally, what I do is add a simple query() method to my database class which just takes SQL is an argument. Then I abstract all my general CRUD methods to the specific class. so if you have an Article class, the the deleteArticle() method would go in that class, and would extend the Article class with the database class. use $db->query($sql) within that class. and so on with your other classes...

seventeen
+1  A: 

This is a pretty common mistake for people new to object-oriented programming. You don't want to extend a class just because it has some functions that you need. In fact, you may want to avoid extending classes at all while you're getting started. Although most intros to OOP make a big deal out of inheritance, I would steer clear for the moment.

Instead, think of how you can group together functions in terms of the way they're used. For example, say you're managing users. You need to keep track of user information, add users, delete them, etc. You could create classes like this:

class User {
  function getName()...
  function getID()...
}
class UserAdmin {
  function addUser(User $user)...
  function getUser($id)...
  function deleteUser($id)...
}

These classes are organized according to concepts, not according to what functions you need to call.

When you do need reusable functions for things like database access, you'll generally want another separate object doing the work. So instead of having your UserAdmin class extend a Database class to format its SQL:

// wrong
class UserAdmin extends Database {
  function getUser($id) {
    $this->openConnection();
    $this->runQuery("select * from users where id = {1}", $id);
  }
}

... you can just use the Database class from within your UserAdmin class:

// right
class UserAdmin {
  function getUser($id) {
    $db = new Database();
    $db->openConnection();
    $db->runQuery("select * from users where id = {1}", $id);
  }
}
class Database {
  function openConnection() ...
  function runQuery() ...
}

Initially it will seem like more work, but it keeps your various classes independent. That makes them easier to write, maintain, and test.

JW