Welcome Guest, Not a member yet? Register   Sign In
extend core class to have constructor input parameters.
#1

[eluser]Hummer[/eluser]
Is it possible to extend a core class to have input parameters in the constructor?

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Model extends CI_Model {

var $input_parms = "";

public function __construct( $params )
{
$this->input_parms = $parms;
parent::__construct();

}
}


When I do this I get this warning:


Severity: Warning

Message: Missing argument 1 for MY_Model::__construct(), called in /var/www/html/codeigniter/system/core/Common.php on line 174 and defined

Filename: core/MY_Model.php

Line Number: 47




Should I be concerned about the warning?

Is the extended class incorrect?

#2

[eluser]Aken[/eluser]
If you want to do this, you'll need to modify the core Loader class to accept the parameters that will be passed to MY_Model's constructor. You can't do it on a default CI installation - gotta modify/extend. Wouldn't be hard to do, though.
#3

[eluser]jvicab[/eluser]
Just declare $params = null (or something like that) in the constructor. That way the $param will not be required to be specified when system scripts use the model, and your code still will benefit from passing the parameter as I assume is what you want, don't you?
#4

[eluser]Hummer[/eluser]
Hi Aken,

thanks for your reply.

I have taken a look at the core Loader class, model method. I cannot see how changing the model method in the loader class will fix the warnings. The warning is coming from where MY_Model is instantiated in the core/Common functions.

The actual model classes that extend MY_Model load with no problems.

#5

[eluser]Hummer[/eluser]

Hi jvicab,

thanks for your reply.

That works very well. Thanks for explaining why it works. That is a good little php tip that I did not know.

Thanks again.
#6

[eluser]InsiteFX[/eluser]
Just create an intialize function
Code:
private $params = array();

public function intialize($params = null)
{
    $this->params = $params;
}

Load your model and then call the models intialize method.

This is how we did it before CodIgniter 2.0.0
#7

[eluser]Aken[/eluser]
[quote author="Hummer" date="1334166282"] Hi Aken,

thanks for your reply.

I have taken a look at the core Loader class, model method. I cannot see how changing the model method in the loader class will fix the warnings. The warning is coming from where MY_Model is instantiated in the core/Common functions.

The actual model classes that extend MY_Model load with no problems.

[/quote]
You're right, the default CI_ or MY_Model class is instantiated in Common.php. However, you cannot utilize parameters in the constructor without modifying the Loader class. Take a look at the code - you'll see it only does new $model(). The advice above is good for fixing your warning, but you still need to add the ability to utilize those parameters.

Or, use the initialize() method as also suggested.




Theme © iAndrew 2016 - Forum software by © MyBB