I have a library in CodeIgniter called "someclass".
class someclass extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('email');
$this->load->database();
// $this->load-> library('database');
}
function email($naar=0,$onderwerp=0,$bericht=0){
if ($naar > 0 && $naar<10) {
$to = "tester@test.com"
//this is the part where it goes wrong
$CI =& get_instance();
$this->load->library('email');
//some one said that i needed ti assign the library's
$this->_assign_libraries();
$this->email->from('tester@test.com', 'test');
$this->email->to($to);
$this->email->subject($onderwerp);
$this->email->message("Test
bericht :
".$bericht);
$this->email->send();
// echo $this->email-> print_debugger();
}
}
}
I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: someclass::$email
Filename: libraries/someclass.php
Line Number: 139
You can have 2 situations in my vision.
- First you can use a helper. Create a .php file (ex: general_helper.php) which can be autoloaded or loaded where you will use it. Autoload if from config/autoload.php with 'general' name.
Here, you can create a method called sendEmail
function sendEmail($naar=0,$onderwerp=0,$bericht=0)
{
$CI =& get_instance();
$CI->load->library('email');
//or autoload it from /application/config/autoload.php
$CI->email->from('tester@test.com', 'test');
$CI->email->to($to);
$CI->email->subject($onderwerp);
$CI->email->message("Test
bericht :
".$bericht);
$CI->email->send();
}
Load your custom library in your controller and call id with sendEmail(....)
You can extend native email library class and create a method inside that class called sendEmail for example:
class MY_Email extends CI_Email {
public function _construct()
{
parent::_construct();
}
public function sendEmail () ..... etc.
}
From your controller load native library class, use $this->load->library('email')
and send your email by calling $this->email->sendEmail(...)
You're doing it all wrong! Are you trying to write your own library? Or are you trying to write a controller for a page?
If you're trying to write a controller, you don't need to run the get_instance()
function. Simply extends the CI_Controller
class, load the appropriate library, in this case it's email
, and use it according to the documentation. Also, controllers are to be put inside the controllers
folder.
But if you're trying to write a library, you can't extend from the CI_Controller
class. You'll also need to grab the instance of CodeIgniter using the get_instance()
function and store it in a variable. After that simply use the object to load the email
library and send the emails.
Also, I don't think you can call a method with an underscore prefix from that class. I'm talking about $this->_assign_libraries()
. In CodeIgniter, a method prefixed with an underscore is a private
function. Thus, you can't access it from within your own class. (Unless of course, it's a protected
function and you extend the class).