Click to See Complete Forum and Search --> : Cannot pass a passed in variable to a function? [PHP5]


Shaitan00
March 29th, 2009, 10:29 PM
I've run into a little problem, chances are the solution is simple but I've yet to uncover the work-around.

I'm trying to take a passed in parameter and pass it into a another function, but it comes out as being null, for example:


The following works fine:

public function get($ID)
{
$myA = new A();
return $myA->foo(1);
}

I can see that in foo() the value passed in is '1' as expected (obviously).

However when I try the following it seems to fail:

public function get($ID)
{
$myA = new A();
return $myA->foo($ID);
}


The difference is that I am passing in $id from get($ID) into foo($ID) - why doesn't this work?
The parameter in foo($ID) comes out as being NULL (unless I am not seeing this right).
Do I need to do like $temp = $ID and pass foo($temp) instead?

Any help would be much appreciated.
Thanks,

PeejAvery
March 30th, 2009, 08:59 AM
You must have something incorrectly setup in your class. It works fine for me. Here's your code modified a little.

<?php
class A {
function foo($param) {
echo $param;
}
}

function get($ID) {
$myA = new A();
return $myA->foo($ID);
}

get("Testing");
?>

Once again, it appears as though you are confusing another languages syntax with PHP. In PHP, you do not declare a function as public unless it is a public function within a class.