Welcome Guest, Not a member yet? Register   Sign In
Calling a function from within a model
#1

[eluser]keld[/eluser]
Hi,

I have 2 functions inside one of my model and I want the first one to call the other one to generate part of the result like this:

Code:
function a($id)
{
     $myarray=array();
     $Q=$this->db->query(...);
     //foreach loop and assign values to the array
     $myarray["stuff"]=b($id);
     return $myarray; //send to controller
}

function b($id){
     $myarray2=array();
     //do things (like another query)
     return $myarray2;
}

So if I do this, i get a php error sayinf that function b() doesn't exist.
Now if I inside function a() i add self:: like so:
Code:
$myarray["stuff"]=self::b($id);

It's working. I just thought self:: was for use in classes, is there a reason for why function b wouldn't be recognized without self?

Thanks.
#2

[eluser]gigas10[/eluser]
Try this:
Code:
$myarray["stuff"]=this.b($id);
#3

[eluser]keld[/eluser]
Nope.
"Message: Use of undefined constant this - assumed 'this'"

I guess I'll keep self:: i would just like to know why...
#4

[eluser]gigas10[/eluser]
Sorry I meant
Code:
$myarray["stuff"] = $this.b($id);

Not too sure if it will work or not Undecided
#5

[eluser]keld[/eluser]
Code:
Fatal error: Call to undefined function b()

Smile
#6

[eluser]gigas10[/eluser]
lol...

That's pretty weird, I do this all the time and I've never encountered anything like that. Sorry I co
#7

[eluser]Colin Williams[/eluser]
Code:
$myarray["stuff"] = $this->b($id);

Are you guys even trying to learn PHP syntax?




Theme © iAndrew 2016 - Forum software by © MyBB