CodeIgniter Forums
Integrate my Classes into CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Integrate my Classes into CodeIgniter (/showthread.php?tid=353)



Integrate my Classes into CodeIgniter - CrazyEddy - 11-24-2014

Hello,

Iam new in the "Codeigniter World".

Im still working at my first "real" Project with CodeIgniter and I Like to add some "Entity" Classes for my Project.
But Iam not sure whats the right way to do that in CodeIgniter.

Here is a Sample Class:
PHP Code:
class UserPayment {

 
   /**
     * @var $id int
     */
 
   private $id;

 
   /**
     * @var $userId int
     */
 
   private $userId;

 
   /**
     * @var $iban String
     */
 
   private $iban;

 
   /**
     * @var $bic String
     */
 
   private $bic;

 
   function __construct(
 
       $id,
 
       $userId,
 
       $iban,
 
       $bic
    
)
 
   {
 
       $this->id $id;
 
       $this->userId $userId;
 
       $this->iban $iban;
 
       $this->bic $bic;
 
   }



I hope you know what I mean.
Sorry for my English Wink

Greats!


RE: Integrate my Classes into CodeIgniter - dmyers - 11-24-2014

If I understand you correctly. Straight up PHP classes are considered CodeIgniter Libraries. So you can just throw it into the lib's folder and call it like this in your controller.
$this->load->library('classname');

Then access it with

$this->classname->foo('hello world');

A few things to note here thou.
#1. the class name and the filename are the same
#2. You get back a singleton.

If you need to just load the class you can use:

$this->load->file('library/classname');
$a = new classname();
$b = new classname();

(If I recall)

If it's something that is accessing a "model" yes you should break it out into a "correct" MVC model but, more than likely it's just easier if it's 3rd party code to keep it all together like a "black box object". Sometime's if it needs multiple additional files I place them in folder in library and create a wrapper in the root level to set it up or just call it in it's folder.

Hopefully that's enough to get to headed in the right direction. Here is the manual page.

http://www.codeigniter.com/user_guide/libraries/loader.html


RE: Integrate my Classes into CodeIgniter - CrazyEddy - 11-24-2014

Hi,

thanks for the answer Smile

PHP Code:
$this->load->file('library/classname'); 

was excactly what iam looking for Smile

Greats