1

I'm using php7cc to check if the certain functionalities work after upgrading from PHP 5.6 to PHP 7.2. It's an old project and that's why it has a lot of legacy code within.

By using php7cc, this is the warning I get:

[Warning] Possible object property creation during by-reference assignment

And the code/line for it is:

$tt_campaign2Obj->local_cObj = &$tt_campaign2Obj->cObj;

I don't understand what is means or if there is any way to suppress those warnings. It appears to be working but I still can't find out why it does give a warning here. Does anyone know better by chance?

Thank you.

6
  • Hi @JayBlanchard yes I did but no answer at all.
    – a.t.
    Mar 6, 2020 at 19:16
  • Really. I'm trying to solve this for a few days and nothing is helping on this problem.
    – a.t.
    Mar 6, 2020 at 19:18
  • This is not my question Jay. I know how to disable warnings/infos or errors.
    – a.t.
    Mar 6, 2020 at 19:20
  • 1
    @JayBlanchard The warning isn't coming from PHP, it's coming from a compatibility checking tool. When I google the message, all I find are bug reports about libraries that encountered the same warning.
    – Barmar
    Mar 6, 2020 at 19:23
  • Ah @Barmar - I think I need a nap. Mar 6, 2020 at 19:28

1 Answer 1

0

This warning happens whenever you assign a reference to a property to another property. There's a similar warning if you assign an array reference to an array element, e.g.

$array[$x] = &$array[$y];

The reason for these warnings is that the order of creation changed in PHP 7. This is explained in the following from https://github.com/php/php-src/blob/PHP-7.0/UPGRADING

  • Array elements or object properties that are automatically created during by-reference assignments will now result in a different order. For example

    $array = [];
    $array["a"] =& $array["b"];
    $array["b"] = 1;
    var_dump($array);
    

    now results in the array ["a" => 1, "b" => 1], while previously the result was ["b" => 1, "a" => 1];

Relevant RFCs:

Unless you actually care about the order of properties in an object or the order of elements in an associative array, this warning can be ignored. If you need the old order, you need to ensure that the property on the right has already been created before the assignment.

I don't think there's a way to suppress it, other than by disabling all warnings by using --level=error. Even if the code does create the RHS property first, I expect you'll get the warning during the assignment, since I doubt it can do the level of semantic analysis to tell whether the property is new.

1
  • Now it makes sense, thank you @Barmar! I guessed something similar but as you said I can ignore it if the order of elements is not important. Also, I you are right about the warning - it will still appear as a "notice" for the user.
    – a.t.
    Mar 6, 2020 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.