Welcome Guest, Not a member yet? Register   Sign In
Using Object and Classes (PHP OO) in CI
#1

[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.
#2

[eluser]jmadsen[/eluser]
most likely

http://ellislab.com/codeigniter/user-gui...aries.html
#3

[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.
#4

[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');

/**
*
*    User class
*    This class holds users that have registered with CitySecret
*/
class User {

    //    
    var $username;
    var $name;
    var $dob;
    var $city;
    var    $email;

    public function __construct($username,$name,$dob,$city,$email) {
        $this->name = $name;
        $this->dob    = $dob;
        $this->city = $city;
        $this->email= $email;
    }

}
?>

Then in my welcome controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
    # NOTE - Nothing heree !!!!    
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

The result when i load the welcome controller is the following:

Code:
A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for User::__construct(), called in C:\xampp\htdocs\citysecret\system\core\Loader.php on line 188 and defined

Filename: classes/user.php

Line Number: 17
A PHP Error was encountered

Severity: Warning

Message: Missing argument 2 for User::__construct(), called in C:\xampp\htdocs\citysecret\system\core\Loader.php on line 188 and defined

Filename: classes/user.php

Line Number: 17
A PHP Error was encountered

Severity: Warning

Message: Missing argument 3 for User::__construct(), called in C:\xampp\htdocs\citysecret\system\core\Loader.php on line 188 and defined

Filename: classes/user.php

Line Number: 17
A PHP Error was encountered

Severity: Warning

Message: Missing argument 4 for User::__construct(), called in C:\xampp\htdocs\citysecret\system\core\Loader.php on line 188 and defined

Filename: classes/user.php

Line Number: 17
A PHP Error was encountered

Severity: Warning

Message: Missing argument 5 for User::__construct(), called in C:\xampp\htdocs\citysecret\system\core\Loader.php on line 188 and defined

Filename: classes/user.php

Line Number: 17
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: name

Filename: classes/user.php

Line Number: 18
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: dob

Filename: classes/user.php

Line Number: 19
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: city

Filename: classes/user.php

Line Number: 20
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: email

Filename: classes/user.php

Line Number: 21

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 Sad ?
#5

[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 {

    //    
    var $username;
    var $name;
    var $dob;
    var $city;
    var $email;

    public function __construct() {
        parent::__construct();
    }

    public function init($name, $dob, $city, $email) {
        $this->name = $name;
        $this->dob    = $dob;
        $this->city = $city;
        $this->email= $email;
    }

}
And in the controller
Code:
$this->load->model('user');
$this->user->init('Dave','01/01/1960','Springfield','[email protected]');
// Do some work
$this->user->init('John','02/02/1970','Shelbyville','[email protected]');
// etc

If you're looking to load multiple users at a time, you could of course do something like:

Code:
class User extends CI_Model {

    //    
    var $username;
    var $name;
    var $dob;
    var $city;
    var $email;

    public function __construct($name = '', $dob = '', $city = '', $email = '') {
        parent::__construct();

        if( ! empty($name)) {
            $this->name = $name;
        }
        if( ! empty($dob)) {
            $this->dob = $dob;
        }
        if( ! empty($city)) {
            $this->city = $city;
        }
        if( ! empty($email)) {
            $this->email = $email;
        }
    }
}
And in the controller:
Code:
// This will assign a blank User object to $this->user, which you can choose to ignore
$this->load->model('user');

$dave = new User('Dave','01/01/1960','Springfield','[email protected]');
$john = new User('John','02/02/1970','Shelbyville','[email protected]');
// etc
#6

[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:
/*
* Payments controller
*
*/

class Payments extends CI_Controller
{
    public function process
    {
        /** form validation could go here... **/

        if($this->input->post('payment_method') == 'paypal')
        {
             $this->load->library('Paypal');
             $this->paypal->setItems('first_name', $this->input->post('first_name') );
             $this->paypal->setField('amount', $this->input->post('amount') );
             echo $this->paypal->autoForm();      
        }

        if($this->input->post('payment_method') == 'credit_card')
        {
             $this->load->library('Credit_card');
             $this->Credit_card->setName($this->input->post('first_name'), $this->input->post('last_name') );
             $this->Credit_card->setCardNumber('card_number', $this->input->post('card_number');
             $this->Credit_card->setCardExpiry('card_expiry', $this->input->post('card_expiry');
             $this->Credit_card->process();        
        }        
        
    }
}

This is how you would go about loading your individual classes.
#7

[eluser]Las3r[/eluser]
Thank you both very much, this has helped me a lot!

Thanks again,

Erik




Theme © iAndrew 2016 - Forum software by © MyBB