Codeigniter加载库并使用别名调用


Codeigniter load library and call with alias name

我想知道在调用库后是否可以使用别名,比如:

$this->load->library('email','em');

我该怎么做?

您可以通过在加载库时提供第三个参数来实现这一点。

如果第三个(可选)参数为空,则库通常会分配给与库同名的对象。例如,如果库名为Calendar,它将被分配给名为$this->Calendar的变量。

如果您喜欢设置自己的类名,可以将其值传递给第三个参数:

$this->load->library('calendar', NULL, 'my_calendar');
// Calendar class is now accessed using:
$this->my_calendar

请参阅Codeigniter的Loader Class文档了解更多信息。

唯一的方法可能与您的after接近,那就是如果您将库加载为

$this->load->library('email');

然后

$em = new Email();
// All the email config stuff goes here.
$em->from('email', 'Name');
$em->to('email');
$em->subject('Test Email');
$message = 'Some Message To Send';
$em->message($message);

$this->load->library('email', NULL, 'em');
// Email config stuff here.
$this->em->from();
$this->em->to();
$this->em->subject();
$this->em->message();