Using Object and Classes (PHP OO) in CI |
[eluser]Las3r[/eluser]
This may sound like a crazy question, but here goes: I've worked with CI in the past, and I fully graps the MCV-pattern. HOWEVER, I would like to create my own classes and use those in conjunction. For example: User classes with all the related methods and fields. But - If I add them to the app/controllers or wherever I found out that: 1) CI expects static classes (User::getAllUsers() for example) 2) I cannot instantiate my own class, __construct will pop an error since CI is trying to construct it immediately. 3) I can't use the following code as "my classes" don't work with CI's internal system if I just included them in the bootstrap. Code: $this->wallet = $this->[model_name]->getTheWalletForUser($this->id) So simple question: WHERE and HOW do I create / mix my class definitions with codeigniters' so that I can use all the models, etc for parsing /fetching DB-related information ? Thanks.
[eluser]danmontgomery[/eluser]
1) no it doesn't, in fact i don't think there's a single static class method in the codebase with the exception of get_instance() 2) yes you can 3) you're doing something wrong Can't help you without code, so post some.
[eluser]Las3r[/eluser]
Noctrum, If I create a class, what class should I extend? If I place a class definition in /models or it will be autoloaded by CI without filling the parameters (without me even instantiating the object. User class (note: I have also tried "extends CI_Model"). This class is autoloaded as a model. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Then in my welcome controller: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); The result when i load the welcome controller is the following: Code: A PHP Error was encountered NOTE: I've also changed the method name to "public function User(xxx)", same issue! It seems to me that CodeIgniter is trying to instantiate it... What am I doing wrong ?
[eluser]danmontgomery[/eluser]
If it's a model, it must extend CI_Model. No models are automatically loaded by CI, so if it's being loaded you either have it in config/autoload.php or are calling $this->load->model('user'); somewhere. (EDIT: Just read that you are autoloading it, so yes... When CI "loads" a class, it also instantiates an object). No parameters get passed to the constructor when models are loaded, but they do get passed to libraries. You can read about that in the user guide. Libraries are just PHP classes, and generally speaking will work without any modification. Assuming your class has database interaction, you probably want to keep it as a model for ease of access to CI's core functionality. If you're unwilling to change your class, however, a library is what you want. As for keeping it a model, I would do something like: Code: class User extends CI_Model { Code: $this->load->model('user'); If you're looking to load multiple users at a time, you could of course do something like: Code: class User extends CI_Model { Code: // This will assign a blank User object to $this->user, which you can choose to ignore
[eluser]andyy[/eluser]
@Las3r: you'll need to create libraries (classes) and load them within your controllers. For example, let's say we have a Payments controller which handles all the payment functions of an e-tailer. I want to load my Paypal library (to handle Paypal payments) and my Credit_card library (to handle credit card payments) depending on the users selected payment method. Both of my libraries are simply classes that I will load within the controller: Code: /* This is how you would go about loading your individual classes.
[eluser]Las3r[/eluser]
Thank you both very much, this has helped me a lot! Thanks again, Erik |
Welcome Guest, Not a member yet? Register Sign In |