Forum Moderators: coopster

Message Too Old, No Replies

Is a general Core class necessary for a PHP site

Like, if Facebook had a Facebook class with site specific functions

         

megaman732

2:37 am on Aug 22, 2009 (gmt 0)

10+ Year Member



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.

eelixduppy

12:44 am on Aug 26, 2009 (gmt 0)



I personally would do it as you have put it here:
 $core->insert_user()
as that seems to be the most useful way to organize it. And for something like this you do not have to extend the database class each time you want to do something like this, you just need to have a database object within that class. For example:

class Example
{

private $db;

// then in the constructor you have to instantiate it
public Example()
{
$this->db = new Database();
// ... etc...
}
}