php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44139 Fatal error with ReflectionFunction and ReflectionMethod using reference
Submitted: 2008-02-17 00:06 UTC Modified: 2008-07-25 01:00 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: andrea at 3site dot it Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrea at 3site dot it
New email:
PHP Version: OS:

 

 [2008-02-17 00:06 UTC] andrea at 3site dot it
Description:
------------
For some reason, ReflectionFunction doesn't allow me to to invoke a basic function like, for example, array_unshift.

Reproduce code:
---------------
$arr = array(2, 3, 4);

/** EXPECTED RESULT
 * array_unshift($arr, 1);
 * array_unshift($arr, 0);
 * var_dump($arr);	// 0, 1, 2, 3, 4
 */

$ref = new ReflectionFunction('array_unshift');

// WARNING: Call-time pass-by-reference has been deprecated
$ref->invoke(&$arr, 1);
$ref->invokeArgs(array(&$arr, 0));
var_dump($arr);	// correct and expected result

// FATAL ERROR - Uncaught exception 'ReflectionException' with message 'Invocation of function array_unshift() failed'
$ref->invoke($arr, 1);
$ref->invokeArgs(array($arr, 0));
var_dump($arr);	// nothing, because of fatal error ... 

exit;

Expected result:
----------------
I would like to do what I'm trying to do without errors.
I mean both fatal and warning.

Actual result:
--------------
An illogical problem for a simple operation.

You have a "passed by reference" in your core but you choosed to disable send by reference in PHP ... and ReflectionFunction doesn't seem to be able to manage this kind of "situation".

Please do not reply with something like: change your PHP configuration ... I would like to write not deprecated code. Thank You.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-17 19:40 UTC] felipe@php.net
PHP 5.2.6:

$ref->invoke($arr, 1);
Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of function array_unshift() failed' 

$ref->invokeArgs(array($arr, 0));
Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of function array_unshift() failed'

PHP 5.3.0:

$ref->invoke($arr, 1); and $ref->invokeArgs(array($arr, 0));
Warning: Parameter 1 to array_unshift() expected to be a reference, value given


 [2008-02-17 19:45 UTC] felipe@php.net
Ops... 5_3 also issued 'Fatal error'.

Warning: Parameter 1 to array_unshift() expected to be a reference, value given in %s on line %d

Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of function array_unshift() failed' in %s:%d
Stack trace:
#0 %s(19): ReflectionFunction->invoke(Array, 1)
#1 {main}
  thrown in %s on line %d

 [2008-02-17 23:17 UTC] andrea at 3site dot it
Hi Felipe, I don't understand your messages.

I know that it generates a Fatal error as I know that send the array forcing reference works as expected but it generates a warning.

As I wrote on php.net manual too, to solve this strange behaviour I used a "non-sense" strategy (at least for me) ...

$arr = array(2, 3, 4);
$ref = new ReflectionFunction('array_unshift');
$ref->invokeArgs(array_merge(array(&$arr), array(1)));
$ref->invokeArgs(array_merge(array(&$arr), array(0)));
// $arr == [0,1,2,3,4]

I can't understand why one single value is accepted by reference inside an array while more than one is not accepted.

Since PHP 5 should manage by itself references I think that ReflectionFunction should do the same, without reference requirement but working as expected.

Do you think am I wrong?
Kind Regards
 [2008-02-17 23:41 UTC] felipe@php.net
$ref->invoke(&$arr, 1);
$ref->invokeArgs(array(&$arr, 0));
var_dump($arr);	// correct and expected result

Doesn't show warning on 5.2.6 and 5.3. That was my message.
 [2008-02-18 09:42 UTC] andrea at 3site dot it
Felipe, I'm testing PHP Version 5.2.6-dev and I have the same problem.

<?php
$arr    = array(2, 3, 4);
$ref    = new ReflectionFunction('array_unshift');
$ref->invoke(&$arr, 1);
$ref->invokeArgs(array(&$arr, 1));
var_dump($arr);
exit;
?>

Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in ...

I guess above warning should not exists. So, can you confirm that this is a bug?
Cheers
 [2008-02-18 09:46 UTC] andrea at 3site dot it
Sorry, this is what I would like to be able to do:

<?php
$arr    = array(2, 3, 4);
$ref    = new ReflectionFunction('array_unshift');
$ref->invoke($arr, 1);
$ref->invokeArgs(array($arr, 1));
var_dump($arr);
exit;
?>

Without fatal errors, simply because if send by reference is deprecated, ReflectionFunction (... and I didn't test ReflectionMethod ...) should be able to manage correctly variables by references.

This is another Fatal Error example:

<?php
function unshift(&$arr, $mix){
    return  array_unshift($arr, $mix);
}
$arr    = array(2, 3, 4);
$ref    = new ReflectionFunction('unshift');
$ref->invoke($arr, 1);
$ref->invokeArgs(array($arr, 1));
var_dump($arr);
exit;
?>

I hope these examples explain correctly where and what is the problem.
Kind Regards
 [2008-02-18 11:33 UTC] andrea at 3site dot it
As expected, this problem is present on ReflectionMethod too.

Basically, the summary of this bug is wrong and I guess that correct could be the changed one.

I tested both problems in PHP 5.2.5 and 5.2.6-dev.

This example shows the problem using methods as well.

<?php

class Test{
    public  function unshift(&$arr, $mix){
        return  array_unshift($arr, $mix);
    }
}

$instance   = new Test;
$arr        = array(2, 3, 4);
$ref        = new ReflectionMethod('Test', 'unshift');

// WARNING
$ref->invoke($instance, &$arr, 1);
$ref->invokeArgs($instance, array(&$arr, 0));
var_dump($arr); // [0,1,2,3,4]

// FATAL ERROR
$ref->invoke($instance, $arr, 1);
$ref->invokeArgs($instance, array($arr, 0));
var_dump($arr);
exit;

?>

Thanks for solving this problem.
Kind Regards.
 [2008-07-25 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 20:01:31 2024 UTC