CodeIgniter Forums
why does loading the library also initializes the constructor? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: why does loading the library also initializes the constructor? (/thread-3511.html)



why does loading the library also initializes the constructor? - El Forum - 10-07-2007

[eluser]dedavai[/eluser]
It seems to me that CI initializes class constructors when it loads libraries with the $this->load->library method. Is there a way to initialize the class when creating new instances?

To clear it up, I'm writing a custom class/library that looks like this:
Code:
class MyFormObjects {
    
    // class globals
    public $formObject;
    private $formTypes = array('text', 'pass', 'textarea', 'select', 'checkbox', 'radio');
    
    public function MyFormObjects ()
    {
        // nothing here
    }
    
    public function __construct($name, $type, $value = '', $description = '', $rules = '')
    {
        if(!in_array($type, $this->formTypes, TRUE)) {
            die("invalid type ($type)");
        } else {
            $this->formObject = array('name' => $name,
                                      'type' => $type,
                                      'value' => $value,
                                      'description' => $description,
                                      'rules' => $rules,
                                      'validationError' => '');
        }
    }
When I try to load the library in my controller...
Code:
$this->load->library('MyFormObjects');
...I get an error because CI expects the $name and $type when loading the class. However, I want to be able to pass those variables during instance initialization, like this:
Code:
$formObject = new MyFormObjects('the_name','text');
Any ideas?


why does loading the library also initializes the constructor? - El Forum - 10-07-2007

[eluser]gunter[/eluser]
I would think the solution to your problem is to include your class the standard php way...
or put it into the plugin folder


why does loading the library also initializes the constructor? - El Forum - 10-07-2007

[eluser]dedavai[/eluser]
Are plug-ins loaded as classes? Seems like they're loading like simple functions, not classes.


why does loading the library also initializes the constructor? - El Forum - 10-07-2007

[eluser]gunter[/eluser]
loading a plugin is like if you include() a file
but you donĀ“t have to care about the path
(but the file has to be in the plugin folder with _pi.php as suffix