Welcome Guest, Not a member yet? Register   Sign In
why does loading the library also initializes the constructor?
#1

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

[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
#3

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB