Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

copyFrom method in PHP8

56 views
Skip to first unread message

Chris Maxwell

unread,
Sep 28, 2024, 11:32:01 AM9/28/24
to Fat-Free Framework
In PHP 7.4 we are using this code when saving some data to a MySQL database with F3's database functions:

$product->copyFrom('POST');

However, with the same code in PHP 8+ it complains about 'Array to string conversion':

AH01071: Got error 'PHP message: Array to string conversion; PHP message: [lib/db/sql.php:137] ; PHP message: [lib/db/sql.php:137] Base->{closure}(); PHP message: [lib/db/sql/mapper.php:101] DB\\SQL->value(); PHP message: [lib/db/sql/mapper.php:688] DB\\SQL\\Mapper->set();

This is only an issue when the POST data is a multidimensional array. If the POST data is a simple array (non-multidimensional) it works fine.

Is there any way to make the copyFrom function work the same as before without erroring in PHP8 or do we need to prepare/filter the POST data so it's not multidimensional with a callback function?

For reference we are using F3 version 3.8.

Thanks,
Chris

PJH

unread,
Sep 29, 2024, 5:43:51 AM9/29/24
to Fat-Free Framework
> However, with the same code in PHP 8+ it complains about 'Array to string conversion':

It should be complaining in php7.4 as well (or, if that's been silenced, it will be giving wrong information - it'll be using the string "Array" instead of whatever value(s) may be being passed):

# php7.4 -e x.php
1
PHP Notice:  Array to string conversion in /tmp/x.php on line 3
Array

# cat -n x.php
    1  <?php
    2  function p($obj) {
    3          echo $obj['val'] . PHP_EOL;
    4  }
    5
    6  $POST_val = ['val' =>"1"];
    7  $POST_arr = ['val' => ["1", "2"]];
    8
    9  p($POST_val);
   10  p($POST_arr);
   11



> Is there any way to make the copyFrom function work the same as before without erroring in PHP8 or do we need to prepare/filter the POST data so it's not multidimensional with a callback function?

You need to create a callback to collapse/resolve whichever item in the POST array is an array itself (POST is already an array - what copyfrom() is not expecting is for any item in that array - matching a key in the object - to be another array.)

Or arrange for the form submitting the POST to not supply whichever item is causing problems to be submitted as an array to begin with.

Chris Maxwell

unread,
Sep 30, 2024, 8:41:13 AM9/30/24
to Fat-Free Framework
Thanks for your response - appreciate it.
Reply all
Reply to author
Forward
0 new messages