开发者

Best way to break up large class into smaller ones in PHP

开发者 https://www.devze.com 2023-01-07 04:20 出处:网络
I\'m mostly an OOP newbie, and am fiddling around in CodeIgniter building a very basic ORM (maybe for future usage - now mostly for learning).

I'm mostly an OOP newbie, and am fiddling around in CodeIgniter building a very basic ORM (maybe for future usage - now mostly for learning).

At this point I have a large class called ORM which my models extend. So a widgets model will have methods like get() (which basically gets all the rows in the 'widgets' table), and get_with_related(), etc.

I'd like to split up this class into a few smaller ones. Maybe if I have a base ORM class, and a 'reader' class (which has get(), etc.), a 'writer' class (which would have update(), save(), etc.), and maybe a utilities class.

What would be the best way to accomplish this? S开发者_如何学编程hould I just include and instantiate the reader and writer classes in the constructor of the base ORM class and use it like this?

$widgets = new Widgets();
$all_widgets = $widgets->reader->get();

$widgets->writer->update('description', 'here is a new description', 357);

Is there a term I can google to find out the ways of organizing classes like this?


I don't know anything about Codeigniter specifically but I don't think it's necessary to break it off into separate read/write classes. In fact, in this instance that might actually be bad practice because in OOP a model is supposed to represent a single entity. You should google DAL (Database Abstraction Layer). If have a Widgets model then it's perfectly ok to have both getters and setters. My point is that it may not be the best practice in your case just to simply break off into smaller classes. This should always be purpose-driven.

0

精彩评论

暂无评论...
验证码 换一张
取 消