Hey everybody,
I was wondering how to deal with plain PHP object and CodeIgniter framework, so here is what I did first :
I created a PHP Class in libraries folder for my object, "User" in that case, with accessors. So far everything was ok with my Class, I tested it in a separate test environement without CI. Then I tried to integrate it to my project so in my controller I included with a
PHP Code:
$this->load->library("User");
But then I was only able to have one instance of User at a time ! That's fine for a User I think but I will have to deal with a lot of instances of some objects and so I "tricked CI" by including in the controller
And that way I was able to have multiple User objects but then I realized that I wanted to use my simple CRUD overriding of CI_Model into my User Class, that way I would be able to make the code for a user authentication very small in controller and easy to understand and also to maintain (yes, it's becoming a huge crap in my controllers, also I splitted a lot my operations into small functions, it's harder and harder to make modifications and most of time it breaks something else when I add/mod something). So, I thinked about it and told myself
Quote:Ok, think about your classes, an Object here represents one or more entries in DB, so It must be between the Model and the Controller.
That way I ended with a User_model but had the same problem with the Loader Class because only one instance was "allowed"... So I made a User Class which extends a User_Model Class which extends MY_Model Class which extends CI_Model Class...............
I don't explain you what CI_Model does... MY_Model :
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Model extends CI_Model {
private $table;
protected function create(__________) {
____________
____________
____________
}
protected function read(__________) {
____________
____________
____________
}
protected function update(__________) {
____________
____________
____________
}
protected function delete(__________) {
____________
____________
____________
}
protected function count(__________) {
____________
____________
____________
}
protected function getTable() {
return $this->table;
}
protected function setTable($table) {
$this->table = $table;
return $this;
}
}
User_model
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends MY_Model {
public function __construct() {
parent::__construct();
if(!class_exists("User")) {
require(APPPATH.'/models/User.php');
}
$this->setTable("users");
}
}
User
PHP Code:
<?php
class User extends User_model {
/**
* Fields
*/
________
________
________
function __construct() {
parent::__construct();
}
/**
* Accessors
*/
________
________
________
/**
* Some usefull functions
*/
public function doLogin($login, $password) {
______________
______________
}
private function authenticate($query, $password) {
______________
______________
}
public function getFromSession() {
______________
______________
}
{ ......... }
}
By the way, I can use $this to call CI Classes like session, db, and others as well as MY_Model functions. I also can change the table while running with the protected accessors for $table field of MY_Model.
Then my login function is a lot easier to understand :
PHP Code:
public function login() {
// Get POST data and check validity
$login = $this->input->post("login");
$password = $this->input->post("password");
if(!empty($login) && !empty($password)) {
try {
$user = new User();
$user->doLogin($login, $password);
$this->layout->view("authentication", array(
"title" => "Logged in",
"message" => var_dump($user->getFromSession()),
"state" => ""
));
} catch(Exception $e) {
switch($e->getCode()) {
case 0:
case 1:
$this->layout->view("authentication", array(
"title" => "Something went wrong...",
"message" => "Please check your credentials",
"state" => "error"
));
break;
case 2:
$this->layout->view("authentication", array(
"title" => "Something went wrong...",
"message" => "Contact data error",
"state" => "error"
));
break;
}
}
} else {
$this->layout->view("authentication", array("title" => "Credentials not provided", "message" => "Please provide both login and password", "state" => "error"));
}
}
SO !
Everything works very well now and I love to abstract a Web Application, objects are so cool
I was just wondering if any of you works with your own-baked Objects. If you are, please share the stuff !
And also, if anyone has anything to say about my approach : tell me, I wan't to here your thoughts about this way of working !
Thanks to read