0

I have the following php errors :

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: t

and

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for myclass::__construct(), called in xxx on line 1011 and defined

My controller contains :

    $this->load->model('mymodel');
    $i = $this->mymodel->func(1);
    print_r($i);

mymodel :

$test = new myclass("foo", "bar");
$myarray[] = $test;
return $myarray

myclass :

class myclass {

var $a;
var $b;

public function __construct($t, $l) // ERROR 2
{
    $this->a = $t; // ERROR 1
    $this->b = $l;
}

}

Note that print_r($a) in controller prints the correct object with content..

Thanks

4
  • PHP gives you the line numbers of the errors. Why don't you post that code… Commented Sep 6, 2012 at 23:46
  • You're right, I edited code with // ERROR in front of concerned lines. Commented Sep 6, 2012 at 23:49
  • 1
    It's not possible. Create some robust 1-file example and put it on pastebin.com so we can run it and see the issue Commented Sep 6, 2012 at 23:52
  • Let's see the code on line 1011. Is $this->load->model('mymodel') constructing myclass? Is $this->model->func(1) constructing myclass? It's hard to tell without a more complete look at the stack. Commented Sep 6, 2012 at 23:52

2 Answers 2

1

Allright, that was caused by Codeigniter.

myclass is declared in a separate file and loaded by mymodel using :

    $CI =& get_instance();
    $CI->load->library('myclass', $params);

When loading the library, Codeigniter creates an instance of myclass as a singleton, but it doesn't transmit any argument. To prevent this error, you have to give parameters before loading the lib :

    $params = array('type' => "a", 'loc' => "b");
    $CI =& get_instance();
    $CI->load->library('myclass', $params);

Note that it must be an array ($params), not a single argument.

Sign up to request clarification or add additional context in comments.

Comments

0

another way is to copy-cat antelove answer.

with that way, you only need change the model file and the way you load the model.

P.S this is just to enrich this SO thread

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.