问题描述
我在CodeIgniter中有一个名为someclass的库。
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 = "[email protected]"
//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('[email protected]', '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.
- 首先,您可以使用助手。创建一个.php文件(例如:general_helper.php),可以自动加载或加载到哪里使用它。
- 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.
在这里,您可以创建一个名为sendEmail的方法
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('[email protected]', 'test');
$CI->email->to($to);
$CI->email->subject($onderwerp);
$CI->email->message("Test
bericht :
".$bericht);
$CI->email->send();
}
在控制器中加载自定义库, code> sendEmail(....)
Load your custom library in your controller and call id with sendEmail(....)
-
本地电子邮件库类和在该类内创建一个名为sendEmail 的方法,例如:
class MY_Email extends CI_Email {
public function _ construct()
{
parent :: _construct();
}
public function sendEmail()..... etc.
}
class MY_Email extends CI_Email { public function _construct() { parent::_construct(); } public function sendEmail () ..... etc.}
从您的控制器加载原生库类,使用 $ this-> load-> library('email')
,并通过调用 $ this-> email-> sendEmail(...)
From your controller load native library class, use $this->load->library('email')
and send your email by calling $this->email->sendEmail(...)
这篇关于代码点火器:负载&在图书馆发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!