Breaking up a large PHP object used to abstract the database. Best practices?

Two years ago it was thought a single object with functions such as $database->get_user_from_id($ID) would be a good idea. The functions return objects (not arrays), and the front-end code never worries about the database.

This was great, until we started growing the database. There’s now 30+ tables, and around 150 functions in the database object. It’s getting impractical and unmanageable and I’m going to be breaking it up.

What is a good solution to this problem? The project is large, so there’s a limit to the extent I can change things.

My current plan is to extend the current object for each table, then have the database object contain these. So, the above example would turn into (assume “user” is a table) $database->user->get_user_from_id($ID). Instead of one large file, we would have a file for every table.

4

First off, forget about tables. Tables are storage. Think about your classes instead. Often classes can be saved in a single table, but not always. So dont think of them as interchangable. They are very different things.

What you want to do is move the database queries into your classes. Keep the database abstraction layer – just use it to execute queries that are passed into it.

So, instead of this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$u = $database->user->get_user_from_id($ID);
</code>
<code>$u = $database->user->get_user_from_id($ID); </code>
$u = $database->user->get_user_from_id($ID);

do this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$u = new User( $ID);
</code>
<code>$u = new User( $ID); </code>
$u = new User( $ID);

The constructor of the User class would handle loading the user from the db.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class User
{
__construct( $id)
{
global $database;
$database->Query( "select name, etc... from users where id = ?", $id);
//load the object from query result
}
}
</code>
<code>class User { __construct( $id) { global $database; $database->Query( "select name, etc... from users where id = ?", $id); //load the object from query result } } </code>
class User
{
   __construct( $id)
   {
      global $database;

      $database->Query( "select name, etc... from users where id = ?", $id);
      //load the object from query result
   }
}

You then extend it so that it can do other tasks:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$u = new User( $ID);
$u->Email = "[email protected]";
$u->Save();
</code>
<code>$u = new User( $ID); $u->Email = "[email protected]"; $u->Save(); </code>
$u = new User( $ID);
$u->Email = "[email protected]";
$u->Save();

Where Save() might be

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function Save()
{
global $database; //there are other ways to do this, but you get the idea
$database->query("update users set ......");
}
</code>
<code>function Save() { global $database; //there are other ways to do this, but you get the idea $database->query("update users set ......"); } </code>
function Save()
{
   global $database;  //there are other ways to do this, but you get the idea


   $database->query("update users set ......");

}

Now you actually have classes doing what they should be doing – the User class worries about users. The Database class worries about the database connection.

What i would recommend to you is to make a database class that knows how to run commands aganst the database and return things in a way you like make it expandable and easy to work with so you can do easy things like the following.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$table('user')->select('*')->where('user_id', 1234, '=')->exec();
</code>
<code>$table('user')->select('*')->where('user_id', 1234, '=')->exec(); </code>
$table('user')->select('*')->where('user_id', 1234, '=')->exec();

that will make it easy to expand that is not the best example but look into active record like suggested, also the one in code igniter is pretty good.

Then extend that class will all the tables that you need to work with. or group them byh the type of call that should make it easy for you to be able to keep everything nice and up to date and have smaller classes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật