开发者

How to Pass Class Variables in a Function Parameter

开发者 https://www.devze.com 2023-03-26 15:03 出处:网络
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->result2co

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 contai开发者_如何转开发ner 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)


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 .


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消