Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:41:08+00:00 2026-05-24T09:41:08+00:00

The main function of the example class uses the reusableFunction twice with different data

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:41:08+00:00Added an answer on May 24, 2026 at 9:41 am

    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 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Qt application that uses a QMainWindow -derived class for the main
Anyone out there used MATLAB's javaObject() function with a JAR file that uses JNI?
I am trying to understand what ThreadPool does, I have this .NET example: class
We have a problem using a class template which itself uses function objects in
Given this example: class Database { private $host, $database, $username, $password, $type; public $active_connection;
I understand the main function of the lock key word from MSDN lock Statement
Does the main function we define in C or C++ run in a process
I don't understand why C#'s Main function is void by default (in a console
What is the correct (most efficient) way to define the main() function in C
I'm building an internal site with the main function of serving up software downloads.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.