简体   繁体   English

如何在 Codeigniter 中使用第三方类

[英]How to use Third Party Class with Codeigniter

So I must be having a brain fart and need a hand.所以我一定是脑子放屁了,需要帮忙。 I'm trying to use BoxPacker in my Codeigniter project to figure out how many items I can fit in some boxes.我正在尝试在我的 Codeigniter 项目中使用BoxPacker来计算我可以在某些盒子中放入多少物品。 I've installed BoxPacker into a "application/third_party/boxpacker" folder.我已经将 BoxPacker 安装到“application/third_party/boxpacker”文件夹中。 But now how do I actually use it?但是现在我该如何实际使用它呢?

For some reason my brain is telling me I have to make my own library to interface with the third party programming but then I just have another brain fart and have no clue how to implement it.出于某种原因,我的大脑告诉我,我必须创建自己的库来与第三方编程接口,但随后我的大脑又放了一个屁,不知道如何实现它。 It's been a long week so I'm pretty burnt out and looking for a hand.这是漫长的一周,所以我精疲力尽,正在寻找帮助。

EDIT: So, I created a library named BoxPacker.php with the following code: `编辑:因此,我使用以下代码创建了一个名为 BoxPacker.php 的库:`

class BoxPacker
{
    function __construct()
    {
        require_once APPPATH."third_party/boxpacker/vendor/autoload.php";
    }
}

In my controller I then call:然后在我的控制器中调用:

$this->load->library('BoxPacker'); $packer = new BoxPacker();

But when I try using the functions in the third part code like below, I get the following error Exception: Call to undefined method BoxPacker::addBox() :但是当我尝试像下面这样使用第三方代码中的函数时,出现以下错误Exception: Call to undefined method BoxPacker::addBox()

$packer->addBox(new TestBox('Le petite box', 300, 300, 10, 10, 296, 296, 8, 1000));

Besides installing the third party library in your application/third_party directory, you must create a new library in application/libraries that requires the third party code and extends it like this:除了在application/third_party目录中安装第三方库之外,您还必须在application/libraries中创建一个需要第三方代码的新库,并像这样扩展它:

Application/libraries/Yourlib.php:应用程序/库/Yourlib.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    // include the 3rd party class
    require_once APPPATH."/third_party/class_name/filename.php";

    // Extend the class so you inherit all functions, etc.
    class NewClass extends ThirdPartyClass {
        public function __construct() {
            parent::__construct();
        }
        // library functions
        public function something(){
            ...code...
       }

Then, on your controller you must load the new library with:然后,在您的控制器上,您必须加载新库:

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

After doing this, you may $this->yourlib as normal这样做之后,你可以像往常一样$this->yourlib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM