• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class include

#1
[eluser]xinq88[/eluser]
Hello everyone,

I'm new to codeigniter, but already have a pretty stupid question :down:.
So the following scenario happened: I have some class which depends on an other class.

So far I've written the following code:

Code:
/* this represents the way i use it in the present */
include_once('uur.php');
/**/

class UserInterface extends Controller {
    
    var $uur;
    var $data;
    var $partials;

    function UserInterface()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
    }
    
    function index()
    {            
        /* this represents the way i use it in the present */
        $this->uur = new Uur();
        $this->content = $this->listUren();
        $this->content = $this->addUren();
        
    }
    
    function addUren()
    {
        // Set de title
        $this->data['title']  = 'uren toevoegen';
        
        // Set de view
        $this->partials = array('content'=>'uren_toevoegen');
        
        // Roep de functie toevoegen aan van de class Uur
        $this->uur->toevoegen('klanttest', '2008-08-05', '2');
        
        // Laad alle gegevens in de template 'main'
        $this->template->load('main', $this->partials, $this->data);
        
    }
}

Code:
class Uur extends Controller {
    
    var $data;
    
    function index()
    {    
        $this->load->database(); // Doesn't work
    }
    
    function toevoegen($klant, $datum, $tijd)
    {
        $data = array(
               'klant' => $klant,
               'datum' => $datum,
               'tijd' => $tijd
            );
            
        //$this->load->database();
        $this->db->insert('uren', $data);
    }
    
    function overzicht()
    {
        //Laad het database model, zodat database functies gebruikt kunnen worden
        //$this->load->database();
        return $this->db->get('uren');
    }
}

I don't think this is the 'codeigniter way' to include a class, because in this situation I can't seem to use the database helper etc. Also the constructor of the class Uur is not called when the following method is called: $this->uur = new Uur();

The output when I don't load the database helper in a specific function (like 'toevoegen') i get the following error while I have the database autoloaded:

Code:
Call to a member function get() on a non-object


I hope my poor English is understandable.

With kind regards,

Nick

(PS: I did google..)

#2
[eluser]xinq88[/eluser]
* little bump *

#3
[eluser]missionsix[/eluser]
Code:
function index()

if this is your constructor, then you've named it wrong. Constructors start with either the class name in php 4+ or __construct() in php5+.

Code:
function Uur() { // php 4
...
}
function __construct() {  // php 5
...
}

If you want to reference the CI core and its inheritance, you need to use this function:

Code:
$ci =& get_instance();

It might be easier to put implement this class as either a model, or a library, depending on what it does. Generally if its interacting with database stuff then you have yourself a model.

#4
[eluser]xinq88[/eluser]
Great, the library functionality does pretty much what I need. Could've known this by reading the manual one more time Tongue.

Still one more problem. As I said, I loaded the database library automatic (in the autoload.php).
When I use a library for my Uur class it doesn't know this library. There's the problem because when I try to load it in the constructor of the class it gives me an error.

Code:
function Uur()
    {    
        $this->load->database();
    }

results in:

Code:
Fatal error: Call to a member function database() on a non-object

When I don't load it at all, the function "return $this->db->get('uren');" returns the following message:

Code:
Call to a member function get() on a non-object in

I have no idea how to do this on the CI way, maybe you can point me in the right direction.

Thank you for your help!

edit:
Code:
function __construct()
    {    
        $this->CI = &get;_instance();
    }

this works for me but is there a way of doing this for all libraries at the same time so I don't have to implement that in every library? Something like what an autoload is for a constructor?

#5
[eluser]xinq88[/eluser]
Here I am again with a question.

Here is the scenario:

I want to create a Controller called MY_Controller extending Controller, so far no problem for me.
But then I want to extend the MY_Controller by 'AnotherController' so. This is not all, at last I want to extend the 'AnotherController' controller.

Code:
class AnotherController extends MY_Controller {
}

class AnotherController2 extends AnotherController {
}

But this: AnotherController2 extends AnotherController
doesn't work, the application can't find the AnotherController class (It does find the MY_Controller class though).

Or maybe I put the files in the wrong dir?
I have them in application/controller/

#6
[eluser]xinq88[/eluser]
Found the solution, I didn't knew I had to load the controller manually.
The following code worked for me:

Code:
require(APPPATH.'controllers/controllerName'.EXT);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.