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 7672565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:18:56+00:00 2026-05-31T16:18:56+00:00

I have a code problem which stems from the fact that I am using

  • 0

I have a code problem which stems from the fact that I am using certain libraries of code I cannot change.

I use the following code to pass execution of any undefined methods to another class, and it works fine but it seems like a waste doubling up.

Any suggestions?

Basically I want to know if it’s possible to pass an unknown number of parameters to a method (without using call_user_func_array(), just in case they need to be passed by reference). I am not asking how to use func_get_args(), rather the reverse.

Or should I just allow for a few more arguments in the first logic path (the list() code)?

class Foo {
    __construct() {
        $this->external = new ClassThatIHaveNoControlOver();
    }

    function bar($name) {
        return 'Hi '.$name;
    }

    function __call($method, $arguments) {
        if (count($arguments) < 3) {
            // call_user_func_array won't pass by reference, as required by
            // ClassThatIHaveNoControlOver->foobar(), so calling the function
            // directly for up to 2 arguments, as I know that foobar() will only
            // take 2 arguments
            list($first, $second) = $arguments + Array(null, null);
            return $this->external->$method($first, $second);
        } else {
            return call_user_func_array(array($this->external, $method), $arguments);
        }
    }
}

$foo = new Foo();

$firstName = 'Bob';
$lastName = 'Brown';
echo $foo->bar($firstName); // returns Hi Bob as expected
echo $foo->foobar($firstName, $lastName); // returns whatever
        // ClassThatIHaveNoControlOver()->foobar() is meant to return

EDIT
Just to clarify, I know I can use this method to rejig the parameters as references, but that would mean passing everything as a reference, even if the method didn’t require it – something I was trying to avoid, but seems unlikely at the moment.

  • 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-31T16:18:57+00:00Added an answer on May 31, 2026 at 4:18 pm

    As commented in the thread question post’s comments this is an example and not necessarily (likely) best practice.

    //Some vars
    $foo = "shoe";
    $bar = "bucket";
    
    //Array of references
    $arr = Array(&$foo, &$bar);
    
    //Show that changing variable value affects array content
    $foo = "water";
    echo $arr[0];
    
    //Sample function
    function fooBar($a)
    {
        $a[0] = "fire";
    }
    //Call sample function
    call_user_func("fooBar",$arr);
    
    //Show that function changes both array contents and variable value by reference
    echo $arr[0];
    echo $foo;
    

    Expanding a bit on the discussion, again not the most industry standard approach but it’ll do the job.

    function pushRefOnArray(&$arr, &$var, $key = false)
    {
        if(isset($key))
            $arr[$key] = &$var;
        else
            $arr[] = &$var;
    }
    

    Essentially you can dynamically build your array and call pushRefToArray() any time you need to pass an item to be passed as reference rather than by value.

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

Sidebar

Related Questions

I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
Problem Hello all! I have this code which takes my jpg image loops through
I have the following code: The actual problem is the non-quoted code. I want
I have some code that I'd like to run on a page. My problem
I have an intermittent problem with some code that writes to a Windows Event
I have a C# web application which generates SVG code using RaphaelJS , which
I have a spurious problem which seems to indicate the index value got from
I'm training code problems like UvA and I have this one in which I
I have a problem where I have code like <table id=table> <tr> <td>...</td> <tr>
Is there any solution to overcome case-sensitive problem for contains method. I have code

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.