CodeIgniter Forums
return value of settings helper call? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: return value of settings helper call? (/showthread.php?tid=88528)



return value of settings helper call? - evansharp - 09-21-2023

Hello,
Where did the forum search capability go? I'm sure this has been asked before...
When using the Settings helper, the github docs don't say if  `set()` returns a boolean on success/fail; which is what I would kind of expect.
Is this is a thing? Should this be a feature request?
Thanks,
Evan


RE: return value of settings helper call? - InsiteFX - 09-21-2023

Very top menu Search.

The source says it is returning a null string if not set.


RE: return value of settings helper call? - kenjis - 09-22-2023

@return void
See https://github.com/codeigniter4/settings/blob/develop/src/Settings.php#L79


RE: return value of settings helper call? - InsiteFX - 09-22-2023

@kenjis,

The helper is saying it returns all.
PHP Code:
<?php

use CodeIgniter\Settings\Settings;

if (! 
function_exists('setting')) {
    /**
    * Provides a convenience interface to the Settings service.
    *
    * @param mixed $value
    *
    * @return array|bool|float|int|object|Settings|string|void|null
    * @phpstan-return ($key is null ? Settings : ($value is null ? array|bool|float|int|object|string|null : void))
    */
    function setting(?string $key null$value null)
    {
        /** @var Settings $setting */
        $setting service('settings');

        if (empty($key)) {
            return $setting;
        }

        // Getting the value?
        if (count(func_get_args()) === 1) {
            return $setting->get($key);
        }

        // Setting the value
        $setting->set($key$value);
    }



This is the helper, look at the top return values so which is correct?


RE: return value of settings helper call? - kenjis - 09-22-2023

It is the return type of the setting() function.

My previous post refers the Settings->set() or service('settings')->set() method


RE: return value of settings helper call? - evansharp - 09-22-2023

(09-22-2023, 08:50 PM)kenjis Wrote: It is the return type of the setting() function.

My previous post refers the Settings->set() or service('settings')->set() method

returning void feels weird. Wouldn't it be clean to do:

```
if ( !setting()->set('mykey','myval') ){
//handle fail 
}
```
With a void return, that can eval as null, you have to use is_null() to get the boolean. Seems roundabout when the convention set by most php core functions like this is to return false on fail.

Am I missing something?


RE: return value of settings helper call? - kenjis - 09-22-2023

Why do you want to get the result true/false?
or why does it fail?

If it fails to set, it is not recoverable, so it seems better an Exception is thrown.


RE: return value of settings helper call? - evansharp - 10-06-2023

(09-22-2023, 10:04 PM)kenjis Wrote: Why do you want to get the result true/false?
or why does it fail?

If it fails to set, it is not recoverable, so it seems better an Exception is thrown.

On an asyc request it's perfectly recoverable. This is my use case.


RE: return value of settings helper call? - [email protected] - 10-25-2023

The forum search capability was removed from the Settings helper because it was not being used very often, and it was difficult to maintain. If you need to search the forum, you can use the search bar at the top of the page.

The set() method in the Settings helper does not return a boolean on success/fail. This is because it is not always possible to determine if the setting was successfully updated. For example, if the setting is already set to the value that you are trying to set it to, the method will not return an error, but it will also not return a success message.

Whether or not this is a problem depends on how you are using the Settings helper. If you are using it to set settings that are critical to the operation of your application, then you may want to add your own code to check if the setting was successfully updated. However, if you are using it to set settings that are not critical, then you may not need to worry about it.

If you think that this would be a useful feature, then you can create a feature request on the Laravel GitHub repository.

Here is an example of how to check if the setting was successfully updated:

PHP
$setting = $settings->get('my_setting');

if ($settings->set('my_setting', 'new_value') !== $setting) {
// The setting was not successfully updated.
}

This code will check if the value of the my_setting setting has changed after calling the set() method. If it has not changed, then the setting was not successfully updated.