The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Few questions about code architecture

Alex's Avatar

Alex

09 Feb, 2012 09:24 PM

Hi. While studying Slim's code I've ran into a couple of questions regarding some of the decisions made at the design / code level.
I'd be glad to understand them.

  1. Why is getMode() called explicitly at the constructor ? (Slim.php line 191)

     //Determine application mode
     $this->getMode();
    

    I understand this is made to populate the $this->mode member, but it will get populated on it's own whenever used for the first time.
    Why is it essential to call this at the constructor and not to postpone until the first use. There is no direct access anywhere to the property.

  2. Casting to string array keys.
    In php absolutely anything may act as array's index. Why is it cast to string here:
    ``` /**

    • Fetch value from array
    • @return mixed|null */ protected function arrayValueForKey( array &$array, $key ) { return isset($array[(string)$key]) ? $array[(string)$key] : null; } ```
  3. is PUT's request must be of application/x-www-form-urlencoded content's type by the specs??
    I think the HTTP verb should indicate what type of request is it, not it's content type.

    /**

    • Get PUT parameters
    • @return array Key-value array of HTTP request PUT parameters */ protected function loadPutParameters() { if ( $this->getContentType() === 'application/x-www-form-urlencoded' ) {

BUT, since Slim mimics PUT requests via a hidden input field, this makes some sense.
Although, when the request is mimiced, all the put values are just being copied from the ->post array in a later stage,
therefore this method seems absolutely unnecessary and real put parameters from real put request wouldn't ever be used.

In other words:
this method checks whether this is a submitted form (mimiced put request)
if yes, it parses the parameters, BUT in a later stage it overrides them by copying the _POST variables
if no, it does nothing (and parameters from real PUT, not mimiced requests, are not utilized)
So what and where am I missing ?

  1. On the other hand, again about lazy loading member's values
    /**
    • Get HTTP request root URI
    • @return string */ public function getRootUri() { return $this->root; } the ->root member is only set in the constructor and never used elsewhere.
      What are the reasons / factors involved at the decision process regarding where and when to populate members?
      In fact, the only thing Request's constructor is doing is initializing variables.

Why (possibly, in theory) wouldn't absolutely any member be set only at the first access (lazy load) when every member has a method?
In all the cases we are talking about protected/private members which are not accessible from the outside.

  1. Support Staff 1 Posted by Josh Lockhart on 09 Feb, 2012 09:34 PM

    Josh Lockhart's Avatar

    In order:

    1. This can certainly be removed from the constructor and delayed until someone first invokes configureMode(). This would remove the overhead entirely if never used.
    2. This has already been refactored in the develop branch and will be rendered obsolete when 1.6.0 is released soon.
    3. That checks if the response body may be parsed using the parse_str function, which expects that format. Again, this has been refactored in the develop branch and will be rendered obsolete when 1.6.0 is released soon.

    As for lazy-loading member variables for various classes, I encourage you to look at the develop branch code. There have been some SIGNIFICANT architectural changes (some described at http://www.slimframework.com/read) that improve and refactor what is available in the current stable branch.

    Version 1.6.0 is just around the corner.

    Thanks for the feedback!
    Josh

  2. 2 Posted by Alex on 09 Feb, 2012 09:49 PM

    Alex's Avatar

    Thanks for the answers Josh. I'll keep exploring on with the 1.6 branch

  3. Support Staff 3 Posted by Josh Lockhart on 09 Feb, 2012 09:56 PM

    Josh Lockhart's Avatar

    Sounds good. And if you still have questions or suggestions for improving things, I'd love to hear them. Feel free to post them here or on IRC.

    Josh

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac