Mike Gerwitz web developer and administrator of GNU/Linux systems, focusing on the development of online communities (e.g. MyCustomBB). Mike is also a freelance web developer and can be found on RentACoder, and is a strong supporter of free software.
Web & Software Developer
18 Dec
In my MyCustomBB Blog a while back, I discussed the PHP’s decision to change the namespace separator to a backslash. Until recently, this patch hadn’t yet been applied. Therefore, I continued developing my code using the old static operator (::).
Well, I recently installed the new PHP 6 snapshot from http://snaps.php.net/ and found that my code was broken - a pleasent surprise I’m sure to many PHP developers. Just think about the ones who don’t even know about the change. Imagine that frustration. Anyway, I quickly realized that the separator had changed. Thankfully, it’s pretty much a simple search-and-replace to modify my namespaces to use the new separator using the sed utility. Be sure to back up your data before performing any batch replacing on your scripts.
I suppose I could help out by providing a script to help you make that change. In my situation, all I have to do is replace “webkernel::” with “webkernel\”, and “cli::” with, “cli\”. Simply paste the below command into a BASH terminal within the root directory of your project, and it will recursively replace all occurances in all PHP files. Again, BACK UP YOUR WORK! I’m not to be held responsible for any irreversible damage done to your code. Be sure you understand the command before running it.
#!/bin/bash
if [ $# -lt 2 ]; then echo Usage: rsed "FILEPATTERN" "REGEXP" exit 1 fi
for i in `find . -name "$1"` do # Make a backup mv $i "$i.bak"
# Perform the replacement sed "$2" "$i.bak" > $i echo $i
# Remove the backup - comment out to keep backup rm "$i.bak" done
echo Done.
Save the above as rsed, chmod +x, and run it using the following as an example: ./rsed “*.php” “s/webkernel::/webkernel\\\\g/” (in the previous command, “\\\\” = “\”). Ensure you use an extension like “*.php” for the file pattern - don’t just use “*” or it’ll cause problems, as it’ll match directories as well. If you have files that contain no extension, replace them manually.
Alternatively, in the above script, you may instead decide to use grep -rl “webkernel::” ./ to modify any file that contains the phrase you’re trying to replace, rather than search for only PHP files (this was more flexible in my case).
NOTE: Be sure to check for strings containing the replaced expression in your code. The replacement may end up escaping part of the string, causing errors (e.g. ‘webkernel::’ to ‘webkernel\’). Good luck!
Leave a reply