SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Python style named parameters

    Is this a decent way to achieve it? I'm not really a huge fan of eval() but it seems to be working well. Are there any possible problems with this solution potentially with long strings that span multiple lines perhaps?

    PHP Code:
    function dictionaryToArrray() {
        
        
    $args func_get_args();
        
        if(empty(
    $args)) {
            return array();
        } else {
            eval(
    '$properties = array('.rtrim(preg_replace('/(.*?):(\".*?\"|[0-9.]*?)(;|$)/s','\'$1\'=>$2,',implode(';',$args)),',').');');
            return 
    $properties;
            
        }

    }

    echo 
    '<pre>',print_r(dictionaryToArrray('a:"My String"','b:45','c:"Some other info"','d:6.4')),'</pre>'
    HTML Code:
    Array
    (
        [a] => My String
        [b] => 45
        [c] => Some other info
        [d] => 6.4
    )

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What's the purpose of this? Where does that list of arguments come from?

    PHP Code:
    $dictionary = <<<EOD
    'a:"My String"','b:45','c:"Some other info"','d:6.4'
    EOD;

    $parts split(","$dictionary);

    foreach (
    $parts as $part) {

      
    $pieces split(":"substr($part1strlen($part) - 2));

      
    $array[$pieces[0]] = $pieces[1];

    }

    echo 
    "<pre>" print_r($array,1) . "</pre>"
    Code:
    Array
    (
        [a] => "My String"
        [b] => 45
        [c] => "Some other info"
        [d] => 6.4
    )

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like a place where json_decode() could be useful.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    This seems more concrete:

    PHP Code:
    function dictionaryToArrray() {
        
        
    $args func_get_args();    
        
    $str '';
        
        foreach(
    $args as $arg) {
            
    $pos strpos($arg,':');
            if(
    $pos!==false$str.= '\''.substr($arg,0,$pos).'\'=>'.substr($arg,($pos+1)).',';
        }
        
        eval(
    '$properties = array('.rtrim($str,',').');');
        return 
    $properties;

    }

    echo 
    '<pre>',print_r(dictionaryToArrray('a:"My String"','b:45','c:"Some other info"','d:6.4')),'</pre>'
    Its going to be used to fill a ActiveRecord object.

    PHP Code:
    $record = new BlogEntry('title:"my title"','weight:0','user_id:2'); 
    vs. (standard way)

    PHP Code:
    $record = new BlogEntry(array('title'=>'my title','weight'=>0,'user_id'=>2)) 

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What about: func( 'title', 'my title', 'weight', 0, 'user_id', 2 ) Also known as a strided list
    Its what is used extensively in SecondLife LSL. http://wiki.secondlife.com/wiki/List#Strided_lists
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    You think that is easier to read? The only reason why I'm doing this is that it can get annoying to pass arrays. I think the "python" like dictionary syntax is much nicer to write am I mistaken? Either way passing the array is the standard way I was just looking to create a alternative.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Oddz, what advantages does using this alternate syntax introduce?

    Surely, the developers using your library would be PHP literate already and comfortable passing an array of values?

    I'm intrigued.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •