Home > Archive > PHP Language > September 2006 > to call only a variable from a function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
to call only a variable from a function
|
|
|
| if I have a function
function X()
{
code...
$array=...;
other code
}
now in a point A of the page I call the
function X;
but how call in other point B, only the
$array ?
| |
| Janwillem Borleffs 2006-08-28, 6:57 pm |
| padew wrote:
> now in a point A of the page I call the
> function X;
> but how call in other point B, only the
> $array ?
>
You mean access the array?
In that case, you should globalize $array:
function X() {
global $array;
code...
$array=...;
....
}
This way, you can access $array without a function in the global scope.
JW
| |
| Colin McKinnon 2006-08-28, 6:57 pm |
| Janwillem Borleffs wrote:
> padew wrote:
>
> You mean access the array?
>
> In that case, you should globalize $array:
>
> function X() {
> global $array;
>
> code...
> $array=...;
> ....
> }
>
> This way, you can access $array without a function in the global scope.
....but its generally considered bad form to clutter up the global scope with
too many variables.
A better approach might be to use a class:
class X {
$array;
function fn()
{
...code...
}
}
$obj=new X;
$function_result=$obj->fn();
$item_from_array=$obj->array[$i];
Alternative you could use a static variable within the functions scope and
multiplex ehaviours:
function X($param, $action="run_code");
{
static $array();
switch($action) {
case "init":
$array=$param;
break;
case "fetch":
return($array);
break;
case "run_code":
...code...
}
}
HTH
C.
| |
|
| On Mon, 28 Aug 2006 20:41:36 +0200, padew <aaaallltttspazzatura@xcxc.xx> wrote:
>if I have a function
>function X()
>{
>code...
>$array=...;
>other code
>
>}
>
>
>now in a point A of the page I call the
>function X;
>but how call in other point B, only the
>$array ?
>
in the function return the array and set it to a var
function X() {
... do a bunch of stuff..
return $array=...;
}
$someArrayValue = X();
it returns an array and it sets it to $someArrayValue
then you can access
$someValue0 = $someArrayValue[0];
$someValue1 = $someArrayValue[1];
$someValue2 = $someArrayValue[2];
see
| |
|
| > ...but its generally considered bad form to clutter up the global scope with
> too many variables.
is bad only if there are many global variables?
ot others?
> A better approach might be to use a class:
>
> class X {
> $array;
> function fn()
> {
> ...code...
> }
> }
only a my think (no sure); must test the code:
this not would work, because the variable take the data by one piece of the
code inner the function fn;
so it musto to be inner the function fn.
| |
|
| Il Mon, 28 Aug 2006 13:21:04 -0700, Gleep ha scritto:
> On Mon, 28 Aug 2006 20:41:36 +0200, padew <aaaallltttspazzatura@xcxc.xx> wrote:
>
>
>
>
> in the function return the array and set it to a var
>
> function X() {
> .. do a bunch of stuff..
> return $array=...;
> }
this not would work, because the variable take the data by one piece of the
code inner the function fn;
it isn't the result of the function , but only take some its data
> $someArrayValue = X();
>
>
> it returns an array and it sets it to $someArrayValue
>
> then you can access
> $someValue0 = $someArrayValue[0];
> $someValue1 = $someArrayValue[1];
> $someValue2 = $someArrayValue[2];
>
> see
| |
| Jussist 2006-09-01, 7:57 am |
| >
> this not would work, because the variable take the data by one piece of the
> code inner the function fn;
> it isn't the result of the function , but only take some its data
>
The academic solution, meaning The Right Way (tm) of doing this, is to
use classes, as said before.
t.j
|
|
|
|
|