views:

45

answers:

2
function first() {
    foreach($list as $item ) {
        ${'variable_' . $item->ID} = $item->title;
        // gives $varible_10 = 'some text'; (10 can be replaced with any number)
    }
    $ordinary_variable = 'something';
}

How to get values of this function inside an another function?

Like:

function second() {
    foreach($list as $item ) {
        get ${'variable_' . $item->ID};
        // getting identical value from first() function
    }
    get $ordinary_variable;
}
  • We know that $variable_id (id is numeric) already exists in first()
  • $list is an Array(), which can have more than 100 values.
  • $ordinary_variable is a string.

Thanks.

+3  A: 

You could let the first function return an array:

function first() {
    $values = array();
    foreach($list as $item ) {
        $values['variable_' . $item->ID] = $item->title;
        // gives $varible_10 = 'some text'; (10 can be replaced with any number)
    }
    $values['ordinary_variable'] = 'something';
    return $values;
}

and then:

function second() {
    $values = first();
    foreach($list as $item ) {
        $values['variable_' . $item->ID];
        // getting identical value from first() function
    }
    $values['ordinary_variable'];
}

or pass it as parameter:

second(first());

I would advice against global as this introduces side-effects and makes the code harder to maintain/debug.

Felix Kling
A: 

${'variable_' . $item->ID} is out of scope. Perhaps you should create a global array and store them there.

simplified example

$myvars = array();

function first() {
  global $myvars;
  ...
  $myvars['variable_' . $item->ID] = $item->title;
}

function second() {
  global $myvars;
  ...
  echo $myvars['variable_' . $item->ID];
}
Fosco
Global is bad and unnecessary. Argument lists exist for a reason.
kevinmajor1
It may be unnecessary, but it's not necessarily bad. No context was provided for the interplay between the two functions.
Fosco
Global obfuscates the interaction between what a function does and what it needs to actually do its job. It leads to debugging, maintenance, and extensibility issues. A function's interface needs to be truthful.
kevinmajor1
In idealistic fantasy land, you're right. I've never been there. Just because something 'can lead' to something, doesn't mean it will, and doesn't mean you should never do it. Ever have a cigarette?
Fosco
Oh please. Just because you've never encountered that situation (yet) doesn't mean that ignoring best practices is a good idea. They're considered best practices for a reason.Yes, _sometimes_ best practices are an idealized goal that shouldn't get in the way of development. 'Global' is not one of those as it doesn't offer anything that passing a variable through an argument list cannot also do. Your code example, for instance, would work just as well by passing the array in by reference. Further, doing that would make it clear to whomever invokes the function that the array is necessary
kevinmajor1
I've encountered the situation, very infrequently. We've both said our piece and should just leave it at that, obviously your mind is closed completely to the thought of ever using a global variable... We shouldn't even bring up the topic of why they are implemented and supported in pretty much every programming language, even the newer ones.
Fosco
Last comment: goto-like structures are also included in most languages, even modern ones. I don't think anyone would say using them is good, or that if one is tempted to use one it's likely a sign of bad design.The only global-like structure I've ever used is a Singleton. And only after careful consideration and care in its use.
kevinmajor1
My 2 cents: Globals are okay to use in a case like this. I wouldn't recommend them as a general tool, though, so I have to agree with @kevinmajor's basic point. Proper OOP is that much better :) I agree though that not using globals often results in using singletons or static classes (which is in no way better in regards to testability and keeping a clean structure) and not using globals *properly* is difficult. I asked a related question a while back that brought a lot of feedback: http://stackoverflow.com/questions/1812472/in-a-php-project-how-do-you-organize-and-access-your-helper-objects
Pekka
and my 3 cents, this solution doesn't work.
Happy
It would if you did it right. Glad you got your solution above, though!
Fosco