1

The main function of the example class uses the reusableFunction twice with different data and attempts to send that data to a different instance variable ($this->result1container and $this->result2container) in each case, but the data doesn't get into the instance variables.

I could get it to work by making reusableFunction into two different functions, one with array_push($this->result1container, $resultdata) and the other with array_push($this->result2container, $resultdata), but I am trying to find a solution that doesn't require me to duplicate the code.

My solution was to try to pass the name of the result container into the function, but no go. Does somebody know a way I could get this to work?

Example Code:

Class Example {

    private $result1container = array();
    private $result2container = array();

    function __construct() {
        ;
    }

    function main($data1, $data2) {
        $this->reusableFunction($data1, $this->result1container);
        $this->reusableFunction($data2, $this->result2container);
    }

    function reusableFunction($data, $resultcontainer) {
        $resultdata = $data + 17;

        // PROBLEM HERE - $resultcontainer is apparently not equal to
        // $this->result1container or $this->result2container when I
        // try to pass them in through the parameter.

        array_push($resultcontainer, $resultdata);
    }

    function getResults() {
        return array(
            "Container 1" => $this->result1container, 
            "Container 2" => $this->result2container);
    }

}

(If this is a duplicate of a question, I apologize and will happily learn the answer from that question if somebody would be kind enough to point me there. My research didn't turn up any answers, but this might just be because I didn't know the right question to be searching for)

3 Answers 3

4

It looks to me like you want to be passing by reference:

function reusableFunction($data, &$resultcontainer) {
    ...

If you don't pass by reference with the & then you are just making a local copy of the variable inside reuseableFunction .

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

5 Comments

Well I feel dumb. I tested various solutions for a couple hours and I was one character away from the start. Thanks Paul.
@Turenne: Please accept this answer, see here: Accepting Answers: How does it work? - thanks for your help!
@Hakre: Just waiting for the 15 minutes to pass and trying to understand why MaSch is advising against using reference.
@Turenne: I think MaSch reminds us that we should be careful with references/variable aliases as they are not always easy to understand/deal with. If you want to learn more about references, the PHP manual has a whole section about them - I can't just explain it in a comment.
@Turenne: I +1 this answer because it's true. I'm wondering though, why passing by reference? Why not just return?
1

You are changing the copy, not the original. Alias the original Array by referenceDocs:

function reusableFunction($data, &$resultcontainer) {
#                                ^

And that should do the job. Alternatively, return the changed Array and assign it to the object member it belongs to (as for re-useability and to keep things apart if the real functionality is doing merely the push only).

Additionally

array_push($resultcontainer, $resultdata);

can be written as

$resultcontainer[] = $resultdata;

But that's just really FYI.

Comments

0

You may pass the attributes name as a String to the method like this:

function reusableFunction($data, $resultcontainer) {
    $resultdata = $data + 17;
    array_push($this->{$resultcontainer}, $resultdata);
}

//..somewhere else..
    $this->reusableFunction($data, 'result2Container')

Some php experts wrote some texts about "why you shouldn't use byReference in php".

Another solution would be to define the containers as an array. Then you can pass an "key" to the method that is used to store the result in the array. Like this:

private $results = array();

function reusableFunction($data, $resIdx) {
    $resultdata = $data + 17;
    array_push($this->$results[$resIdx], $resultdata);
}

//..somewhere else..
    $this->reusableFunction($data, 'result2Container');
//..or pass a number as index..
    $this->reusableFunction($data, 1);

4 Comments

heres a link to a blog post form Johannes Schlüter (Release Manager of PHP 5.3): "Do not use PHP references" schlueters.de/blog/archives/125-Do-not-use-PHP-references.html
The link you give is indeed a good read, but do not over-generalize: "Summary so far: - Do not use references for OO but get ridof PHP 4 legacy. - Do not use references for performance.". It's not a general "never do this" but a "know what you do" reminder Johannes gives. If indeed one has no clue of what references are, do not use them.
I dont really see a reason to use references (&) in PHP but some really good reasons AGAINST it. Everything is better without them!
If you are interested I might pick some of my answers on SO and link them here that show some usage of references of some corner-cases. But only if you like. I'm generally with you and Johannes that you should know them good enough to not use them (and for the spare cases there are, you know them good enough for what you can use them).

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.