Welcome Guest, Not a member yet? Register   Sign In
question about pre-loading libraries in constructors
#1

[eluser]b.rad[/eluser]
Newbie here. I started pulling together an application about a year ago with some success, but had to put it down; I'm restarting and basically relearning from scratch.

As training wheels, I'm walking through this recommended demo re: building an app from scratch.
http://www.phpandstuff.com/articles/code...ignup-form

Problem is, I'm seeing inconsistent behavior with what is described, and I've been digging around for a while now without finding answers....so I figured I'd turn to the forum.

In the demo, the author suggests pre-loading libraries in the __constructor function, as per this simplified code example:
Code:
<?php

class Signup extends Controller {

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

        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
    }

    public function index() {
        $this->load->view('signup_form');
    }

}

The problem is that the helper and library are never loaded. I have to add the library call directly to the index function in order to get the form_validation working, and as I extend the demo I'm having to put it in other functions as well. This seems incorrect, but I can't figure out what's going wrong. In the comments to the article someone else had the same problem...

So the question is, how do I correctly pre-load libraries into a controller, such that those libraries can be successfully used in all other functions. The expected behavior would be this:
Quote:Note: When you load Helpers and Libraries inside the Constructor, they become available in all methods of that Controller. For example, now index() function can use them. Note2: When Views are loaded, they also can use the Helpers and Libraries that the Controller had loaded.

I'm sure I'm missing something trivial, but I'm probably not the only one with this problem.

Any insight is greatly appreciated.
Thanks in advance.
-b.rad
#2

[eluser]tonanbarbarian[/eluser]
i believe, and i could be wrong here, it is an issue between php4 constructors and php5 constructore

if you use the php4 constructore syntax, i.e. a method with the same name as the class, I believe that the example will work (always has for me)
however if you use the php5 style constructor syntax, i.e. a method called __construct

so trry
Code:
<?php

class Signup extends Controller {

    public function Signup() {
        parent::Controller();

        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
    }

    public function index() {
        $this->load->view('signup_form');
    }

}

I think it has something to do with the order that code is processed by both PHP, i.e. __construct may be processed before the Signup method, and also it may have something to do with how the CI Core is coded.
It may also have something to do with the version of PHP you as using as well.

I am sure someone else can confirm or deny my suppositions
#3

[eluser]Alexandru M.[/eluser]
Both methods "should" work , I have been using both __construct() and the class name function and they worked.
However , check if your php version is 5+ before using __construct() as I am not sure if it works on php 4.x .
Besides , why not use the autoload.php file to load up the helpers and libraries?
#4

[eluser]b.rad[/eluser]
@tonan, your approach is working for me -- using a method with the same name as the class. So I guess it must be a syntax thing. I'll use your approach until I hear differently, but I'd be interested to know what the "right" approach is... Smile I'm using PHP 5, fwiw.

@alexandru, for my demo I could certainly autoload these libraries, but I think the idea is to selectively load libraries as you need them -- if possible -- to speed efficiency and execution time. Interestingly, as noted above, the __contruct() method *is not* working for me on php 5.x...

Thanks to you both for your help.
#5

[eluser]nuwanda[/eluser]
Code:
function __construct(){
        
  $this->load->library('library');
    
}
        
        
function index(){
            
  if ($this->library->method) {

    do something
  
  }

}

Works just fine.

So does this:

Code:
function index(){

  $this->load->library('library');
            
  if ($this->library->method) {

    do something
  
  }

}

I do it this way all the time.
#6

[eluser]Alexandru M.[/eluser]
Honestly , I'd rather have the database , authentication library and url helper autoloaded in all my apps instead of loading them in the controllers (purely out of comodity).
I load my image editor in controllers tough , because I don't need it in most of the application's controllers , only in 2-3 tops.
BTW , what are the performance hits of autoloading 2-3 libraries ? I searched for some numbers but did not find any.
#7

[eluser]nuwanda[/eluser]
Sure, load your libraries as needed by your methods, but if several methods in your controller need the same library, it makes sense to load it in the constructor.

Understand that libraries are loaded on the server, and they load very quickly. I used to look at massive libraries and shudder thinking, yikes, that's a lot of code. But simply loading big scripts server-side is not really a bottleneck. They load insanely fast. It's the processing and output of the library that creates lag.




Theme © iAndrew 2016 - Forum software by © MyBB