Reputation: 6852
I have some classes, that I want to load as a model, but problem is I want to separate them in several models, now I have one file with classes that looks like this:
class email {
function add($email, $name, $quiet=NULL, $actiovation=NULL) {
global $secretstring;
global $mail;
global $path;
global $activating;
if (strlen($email) < 1) {
if (!isset($quiet)) {
msg::getInstance()->addSuccess("Please enter your email address.");
}
$error = 1;
}
if (strlen($name) < 1) {
if (!isset($quiet)) {
msg::getInstance()->addSuccess("Please enter your name.");
}
$error = 1;
}
$addData = mysql_fetch_array(sql::getInstance()->query("SELECT id FROM emails WHERE email='".sql::getInstance()->sec($stamp)."'")); // getting id of this email
}
class msg {
static private $instance = NULL;
function addSuccess($success) {
$this->success .= $success."\\n ";
}
}
class sql {
static private $instance = NULL;
function query($query) {
return mysql_query($query);
}
function sec($string) {
return mysql_real_escape_string(htmlspecialchars($string));
}
With instance I can easily call function from another class? But problem is when I want to call function from another model, in one model, I don't know how to make that in CI? Any help I have made a simple example to show how I made classes.
Upvotes: 1
Views: 8416
Reputation: 2676
CI has two methods to call functions. Whatever model you create, you have to load that model in that class where you want use that class.
Manual Loading.
$this->load->model("your_model_name");
Note: Call above line in that class where you want use.
Load your class in the auto_load class placed in application/config/auto_load.php
Calling of function :
$this->your_class_name->function_name(parameters);
Read these resources for more help and clarification.
Upvotes: 0
Reputation: 1122
You just need to make sure you load the model of which you want to call function in the model that you want to use those functions in. Just like you would do in the controller.
Something like this:
public class Email extends CI_Model{
function add(...){
$this->load->model('msg');
$this->msg->addSuccess(...);
}
}
It's really as easy as that.
Upvotes: 4