mardi 4 octobre 2022

How do you decide if a function should return data directly (via return statement) or indirectly (e.g. updating a class property) or both? [closed]

I have some functions which update records in some database tables.

When I run each function I want to know the number of records affected by each.

I'm wondering what the most elegant way of returning this information is.

Do I return it directly from the functions as an integer like this?

$result1 = $records->create();
$result2 = $records->update();
$result3 = $records->delete();

echo "1: $result1";
echo "2: $result2";
echo "3: $result3";

Do I return it directly using a labeled array like this?

$result1 = $records->create()["number_created"];
$result2 = $records->update()["number_updated"];
$result3 = $records->delete()["number_deleted"];

echo "1: $result1";
echo "2: $result2";
echo "3: $result3";

Do I return it indirectly by updating a class property like this?

$records->create();
$records->update();
$records->delete();

echo "1: " . $records::number_created;
echo "2: " . $records::number_updated;
echo "3: " . $records::number_deleted;

Or do I use a combination of the above, returning it directly and also using class properties?

When would each case be used, and how would you decide which approach to use?

Aucun commentaire:

Enregistrer un commentaire