Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing ArrayAccess for Config #2391

Closed
wants to merge 5 commits into from
Closed

Conversation

ragboyjr
Copy link
Contributor

@ragboyjr ragboyjr commented Apr 7, 2013

This syntax allows for less verbose accessing of config items. The only issue I see is that offsetGet will return NULL if it doesn't exist whereas $this->config->item('item'); will return false.

…guide_src accordingly

Signed-off-by: RJ garcia <rj@bighead.net>

public function offsetExists($key)
{
return isset($this->config[$key]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything that checks for array offsets should use array_key_exists() instead of isset(), due to false negatives from NULL values.

Related: the config class' item() method has this problem, too, which should be fixed.

@narfbg
Copy link
Contributor

narfbg commented Apr 7, 2013

CI's styleguide doesn't accept camelCase. And of what use is this anyway?

@TheDigitalOrchard
Copy link

@narfbg, when implementing ArrayAccess, those methods, which are written in camelCase in the documentation, need to be implemented.

I'm split on whether this should be included in CI. It sets a bad precedence for configuration for other areas of CI, such as individual libraries, etc. I think it should remain in a fork or overloaded Config library, but then again, there's no way to implement it in a class extension anyway.... hmmm.


public function offsetExists($key)
{
return array_key_exists($this->config[$key]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing second parameter.

@AkenRoberts
Copy link
Contributor

@TheDigitalOrchard Sure you can.

class MY_Config extends CI_Config implements ArrayAccess {}

@vlakoff
Copy link
Contributor

vlakoff commented Apr 13, 2013

By the way, shouldn't config->item('item') return NULL instead of FALSE if not set, as it has already been changed in load->get_var() ?

@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 6, 2013

Um, sorry for the period of silence, but I'm fine if this feature doesn't get implemented; however, it's nice syntactic sugar, doesn't affect runtime performance, doesn't break old code, and doesn't affect people who don't want to use that feature, and it's a pretty neat feature for people who want it. If that argument doesn't sound that great to everyone else, then I'm totally fine with closing the ticket.

@AkenRoberts
Copy link
Contributor

If it is to be accepted, you still need to make a couple updates to make it ready to merge.

Signed-off-by: RJ garcia <rj@bighead.net>
@narfbg
Copy link
Contributor

narfbg commented May 17, 2013

I'm still having a hard time understanding this and the included documentation changes doesn't make it any clearer. If I can't understand the benefits, how could a newbie reading the docs do?

@ragboyjr
Copy link
Contributor Author

This feature is primarily for syntactic sugar. We can update the documentation to be clearer, but it's just a simpler and more concise way of accessing the main config array. It doesn't affect runtime performance, and it's the same time complexity as using Config::item(). So the advantages would be simpler, and concise syntax for accessing config values. The disadvantage would be that it might confuse new comers to ci, however I think we can combat that last point by making documentation clearer, and if it still confuses a certain newcomer, they aren't forced to use this syntax, they could use item function.

@narfbg
Copy link
Contributor

narfbg commented May 18, 2013

OK, consider me a complete newbie and answer to these questions:

  • How is it different than Config::item()?
  • If it does the same thing as Config::item(), why should I use it?
  • Can I have an example?

@vlakoff
Copy link
Contributor

vlakoff commented May 18, 2013

FYI, I proposed for config->item() a change of the return value for nonexistent items: #2449.

@TheDigitalOrchard
Copy link

  1. Config::item() should indeed be returning NULL if a config key doesn't exist. If this isn't yet happening, then it should be fixed.
  2. Implementing ArrayAccess would be useful in cases where you want to concatenate a config-stored value into a string, but that's the only use that I can think of where Config::item() doesn't already meet the need.
$str = "This string is {$this->config['str_state']}";

vs:

$str = "This string is ".$this->config->item('str_state');

How often that feature would be used in this way is to be established.

@AkenRoberts
Copy link
Contributor

Basically, there's no new value or feature added, it just gives users another option for how to access / define config items. It's possible that some people would prefer array notation; this would give them the option.

@ragboyjr
Copy link
Contributor Author

@cryode and @TheDigitalOrchard is correct. It's pretty much syntactic sugar, it doesn't affect runtime performance, using it is just efficient as Config::item(), and IMHO, it's nice, concise way to access config items; I also think it makes sense to use this array notation because the Config class is really just a wrapper around the main config array (and sub arrays that are loaded).

@TheDigitalOrchard
Copy link

@ragboyjr - it appears you added a commit with totally unrelated changes from this pull request. Was that a mistake?

@ragboyjr
Copy link
Contributor Author

@TheDigitalOrchard, I'm pretty sure that Merge was because I was just updating my CI repo to the latest in development

@narfbg narfbg closed this Jan 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants