Handy Textmate command for PHP developers

This code snippet which can be added to Textmate as a command will convert PHP object variable instances to associative array variables (and vice versa).

For example it will convert :

$thevar->somevar->anothervar

to :

$thevar['somevar']['anothervar'];

This comes in handy while developing in PHP environments where both variable forms are used and if you mistakenly used the wrong form.

Here’s the code :

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env ruby
 
str = STDIN.read
case str
 when /\$.+(\[["'].+["']\])+?/
   print str.gsub(/\[('|")/, '->').gsub(/("|')\]/, '')
 when /\$.+(->.+)+?/
   parts = str.split("->")
   print parts.shift + "['" + parts.join("']['") + "']"
 else
  print str
end

You can add it to Textmate by choosing the menu item Bundles -> Bundle Editor -> Show Bundle Editor. Click on the screenshot below for more details :

textmate-toggle-class-object.png

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
Copyright © 2007 Sadglobe. All rights reserved.