Fatal error: Can't use function return value in write context

Fatal error: Can't use function return value in write context

by Vid Vrh -
Number of replies: 7

Hey all,

I am working on a plugin that requires interaction with the database. What I want to do is to programatically add custom profile fields by adding records to the "user_info_field" table in the database, which I successfully do by doing 

$DB->insert_record('user_info_field', $polje); 

,where $polje is the object with all the data to add to table columns. My problems start when I try to access the id of the inserted record which is returned by the function which I verified by doing 

$polje->id = $DB->insert_record('user_info_field', $polje);

echo $OUTPUT->notification($polje->id); 

, which works just fine. However, I can't assign the returnet id nither to an array (which is the ultimate goal), neithe to a normal variable. I have also tried to do

$polje->id = $DB->insert_record('user_info_field', $polje);

$id = $polje->id

, but whatever I do, I get the error "Fatal error: Can't use function return value in write context".

This error is driving me nuts. I have searched the web for answers but all I have found is a lot of answers that this is some weird error which occured in PHP versions before PHP 5.5, which I am definetely not using. 

If it helps, I am using Moodle 3.11.6+ (Build: 20220405) with PHP 7.4.29, running on an XAMPP server,

Can someone please help me with that? 

Vid 


Average of ratings: -
In reply to Vid Vrh

Re: Fatal error: Can't use function return value in write context

by Syed Nayab Bukhari -
Picture of Core developers

Are you trying to update same object which is used for insert?

Try

$Id =$DB->insert_record('user_info_field', $polje);

$polje->Id = $id

//     try below to confirm insert results.  

print_object($id); 

Happy to discuss further.

In reply to Syed Nayab Bukhari

Re: Fatal error: Can't use function return value in write context

by Vid Vrh -
Thank you for your answer.

I tried the code you sent and now the reurned id is assigned to the $polje->Id value, the variable $id is empty when I try to print it with the print_object($id). However, when I try to assign the value $polje->Id to an array, I still get the same error.

Any ideas?
In reply to Vid Vrh

Re: Fatal error: Can't use function return value in write context

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Can you post the rest of your script (not here, use something like a GitHub Gist and link to it)? That error usually relates to something being passed to a function that doesn't like it, so it's possible that it's actually another line of the file causing the error, rather than $id = $polje->id.

In reply to Mark Johnson

Re: Fatal error: Can't use function return value in write context

by Vid Vrh -
Thank you for your answer.

Link to the script is: https://gist.github.com/vrhvid/6c33caf99cd590d5929ad055745d7784

The error gets thrown at line 76.

Any ideas?
In reply to Vid Vrh

Re: Fatal error: Can't use function return value in write context

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

From the looks of it, you are trying to assign $polje->id to an array element, but you've got the syntax wrong. $customfields('Smer') will try and call a function with the name stored in $customfields, you probably want to do $customfields['Smer'] instead.

In reply to Mark Johnson

Re: Fatal error: Can't use function return value in write context

by Vid Vrh -
So it turns out I am an idiot who should not be allowed to tuch any code ever again.

Honestly i didn't see that I messed up the brackets for some reason and would be banging my head against the wall forever if you didn't point it out to me.

Thank you for your help and sorry for bothering you with such stupid mistake.
In reply to Vid Vrh

Re: Fatal error: Can't use function return value in write context

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I wouldn't be too hard on yourself, this is an odd case where a syntactical error results in valid (but very odd) syntax, so you don't get the obvious "unexpected (" error you might expect. This is one of those "You probably don't want to do this" situations that CodeSniffer could catch, though...