Object-oriented – OOP – Separate Manager class

object-orientedPHP

I have started using OOP in PHP recently. For every model, I create two classes. One class for read operations and another Manager class for all the create / update / delete operations. For example,

<?php
// This is a sample class, just to convey the idea
class Image {
    public static function loadById() {}
    public static function loadByName() {}
    public static function search() {}

    public function __construct () {}
    public function __get () {}
    public function getData() {}
    public function getRating() {}
    public function getSimilar() {}
    public function getRelatedNews() {}
}

class ImageManager extends Image {
    public function __construct() {}
    public function __set() {}
    public function remove() {}
    public function update() {}
    public function save() {}
    private function updateSomethingElse() {}
}
?>

The reason I separated this into two classes was that I thought it was a bit more secure, since it did not expose the C/U/D methods to a normal user. Also, I thought this might be faster / memory efficient since I am loading only the smaller class for 95% of the requests. But now I think these reasons are too insignificant for maintaining two files.

Is this a bad practice? Normally, the Manager classes are loaded only during admin operations. User actions rarely update the database (except maybe during a rating operation, or a cron job for updating view count). Should I combine both the classes into the Image class?

Best Answer

This looks a bit like a DCI architecture. So similar structuring is used by some people . It has some advantages related to security and having to "maintain" more files is often enough the better thing compared to a single file with lots of functions. I split them sometimes just for that reason.

Though I wouldn't expect much for speed or memory. Your framework or whatever you use should cache your project in memory anyway and loading a single image is most likely using more memory and taking more time than those few bytes.