Skip to content

PHP upgrade

PHP4 style class constructor

Upgrading from older versions of php which have the old style constructors can be done using this script.

#!/bin/bash

classes=$(grep -r -E "^class [_a-zA-Z0-9]" * | sed -r -e 's/(.*):class\ ([^\ {]+).*$/\2:\1/' | sort | uniq)

for line in $classes; do
  split=(${line//:/ })
  class=${split[0]}

  if [ "${last_class}" = "${class}" ]; then
    continue
  fi
  last_class="${class}"
  echo $class   ----   ${split[1]}

  sed -i "s/function ${class} *(/function __construct(/" ${split[1]}

  find * -type f | egrep "\.php|\.inc" | while read file; do
    if [ "${class}" != "xml" ]; then
    sed -r -e "s/${class}::${class}/${class}::__construct/" -i "$file"
    fi
  done

done

ereg() and eregi()

You should replace there two with preg_match(). For case insensitive add an i after the pattern. Example:

preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $email);
How to replace ereg with preg_match

Tracking down shortcode usage

CS fixer can fix this. link

Alternative: use this in the project root. It will ignore the blog which is usually wordpress.
(The script doesn't find shortcodes followed by an immediate line break)

find ./ ! -ipath "*/blog/*" -iname "*php" -type f -exec grep --color=always '<?[^p|x|=]' {} +