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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:19:07+00:00 2026-06-06T19:19:07+00:00

class a { public function f(&$ref1, &$ref2) { $ref1 = ‘foo’; $ref2 = ‘bar’;

  • 0
class a
{
    public function f(&$ref1, &$ref2)
    {
        $ref1 = 'foo';
        $ref2 = 'bar';
    }
}

class b
{
    public function __call($methodName, $arguments)
    {
        $a = new a();
        call_user_func_array(array(
            $a, $methodName
        ), $arguments);
    }
}

$ref1 = 'X';
$ref2 = 'Y';
$b = new b();
$b->f($ref1, $ref2);
var_dump($ref1, $ref2);

This results in:

PHP Warning:  Parameter 1 to a::f() expected to be a reference, value given in /home/jon/sync_workspace/bugsync/tests/test.php on line 18
PHP Stack trace:
PHP   1. {main}() /test.php:0
PHP   2. b->f() /test.php:23
PHP   3. b->__call() /test.php:23
PHP   4. call_user_func_array() /test.php:17
string(1) "X"
string(1) "Y"

How can I accomplish the above in PHP 5.4 (manipulate ref1 and ref2 by use of reference)?

In PHP 5.3 I used the & syntax at $b->f(&$ref1, &$ref2); (even though it’s deprecated), but in PHP5.4 this throws fatal error.

  • 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-06-06T19:19:09+00:00Added an answer on June 6, 2026 at 7:19 pm

    I managed to find a solution, although it’s a hack.

    You can still store references in an array, and pass the array as an argument, which will survive through __call()

    class b
    {
        public function __call($methodName, $arguments)
        {
            $a = new a();
            call_user_func_array(array(
                $a, $methodName
            ), reset($arguments));
        }
    }
    $ref1 = 'X';
    $ref2 = 'Y';
    $b = new b();
    $b->f(array(&$ref1, &$ref2));
    

    PHP manual states: Function definitions alone are enough to correctly pass the argument by reference. (http://php.net/manual/en/language.references.pass.php) which clearly isn’t the case for __call() referenced functions!

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

Sidebar

Related Questions

class test { public $isNew; public function isNew(){} } $ins = new test(); $ins->isNew
I would like to do something like this: abstract class Foo { public function
class posts { public function __construct($db) { $this->dbase = $db; $test = new post(1,
With an example class such as this: class Test{ public function &__get($name){ print_r($name); }
Lets say I have these classes: class Foo { public $_data; public function addObject($obj)
echo 'test'; class createdclass { public $name; } class testc { function &testm(){ $myvar
I have my class public function convert( $title ) { $nameout = strtolower( $title
<?php class test_class { public function __construct() { } public function doLogin($username,$password) { include(connection.php);
class t { public function tt() { echo 1; } } t::tt(); See?The non-static
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;

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.