Reputation: 31
I have a background in Java, lately I learned a the theory behind some web development language, while my back-end language is PHP. In addition, I started to working with CodeIgniter (I'm not sure if it does matter to my question, but anyway).
So, I started to build a simple galleries system - but not sure if the architecture is right (I based on my background in Java, but I don't know if web development is the same).
The galleries system is very standard: user can upload / delete images and galleries, view gallery, view image and view all galleries (in this case the name of the gallery is displayed and thumb of the last added image). there is pagination at the all 'views'.
I created 3 classes, under application/libraries/galleries
:
gallery_actions.php:
class Gallery_Actions {
// Nuber of galleries to display in one page
const galleriesPerPage = 4;
public function getGalleries($page) {
$q = .. query ..;
return $this->getObjectGalleryArray($q->result_array());
}
public function getFeatureGallery() {
$gallery = .. query .. ->row_array();
return new Gallery($gallery);
}
public function getPopularGalleries($limit) {
$q = .. query ..;
return $this->getObjectGalleryArray($q->result_array());
}
// Get database galleries array and return object galleries array
private function getObjectGalleryArray($q = array()) {
$galleries = array();
foreach ($q as $gallery) {
$galleries[] = new Gallery($gallery);
}
return $galleries;
}
}
gallery.php:
class Gallery {
// holds gallery info from DB
// int id, varchar(255) name, varchar(255) lastImg, int countImgs
public $config = array();
// Nuber of imgs to display in one page
const imgsPerPage = 12;
// In most cases (maybe at all), gets all config. sometimes only id.
function __construct($params = array()) {
if (count($params) > 0) {
foreach ($params as $key => $val) {
$this->config[$key] = $val;
}
}
}
// Watch inside gallery
public function getGallery($page) {
return array[
'info' => $this->config,
'images' => $this->getImagesOfGallery($page)
];
}
// Watch when browse galleries
public function getPreview() {
return array[
'name' => $this->config['name'],
'lastImg' => new Gallery_Image($this->config['lastImg']),
'url' => $this->config['url']
];
}
private function getImagesOfGallery($page) {
$q = .. query ..;
$imgs = array();
foreach ($q->result_array() as $img) {
$imgs = new Gallery_Image($img);
}
return $imgs;
}
public function create() { .. }
public function uploadImages() { .. }
public function delete() { .. }
private function updateCount() { .. }
}
Gallery_Image:
class Gallery_Image {
// holds img info from DB
// int id, varchar(255) name, varchar(255) url
public $config;
// In most cases gets all config OR only id.
function __construct($params) {
if (count($params) > 0) {
foreach ($params as $key => $val) {
$this->config[$key] = $val;
}
}
}
public function getImage() { return $this->config; }
public function update() { .. }
public function delete() { .. }
public function getThumb() { .. return url string .. }
}
It's a little long, but it's really not hard to understand. To be honest, I wrote the code just now, so maybe there are syntax errors - but that not the point. The point is the mixing of the OOP at the code && web development. The big advantages of this code is that there isn't duplicated code, very clearly, actualize the idea of OO. WHAT I'M NOT SURE - is the creation of the objects necessary and effective? I mean, web development is the same architecture method such as, for example, building a game for android phone? each table in the database (of course not binding tables etc) has a php class and object?
Upvotes: 3
Views: 220
Reputation: 241
The web is a delivery mechanism, nothing more. See: http://www.youtube.com/watch?v=WpkDN78P884
As you said, building a game for android and building a web application often have similar architectural challenges. There is no architectural silver bullet - the form and intent of your code should inform each other.
The creation of objects is never necessary - some programmers would prefer to write web apps in assembly! However, objects can be a valuable abstraction. As you said, "The big advantages of this code is that there isn't duplicated code, very clearly, actualize the idea of OO." The important advantages are code readability and maintainability, which are both (almost totally) subjective. It is up to you as the maintainer of the codebase to decide what is most effective for you.
Now, if you are looking for web app architecture OPINIONS, check this out: http://12factor.net/
Upvotes: 1