{"id":623,"date":"2015-04-22T23:27:51","date_gmt":"2015-04-23T03:27:51","guid":{"rendered":"https:\/\/www.devolve.net\/blog\/?p=623"},"modified":"2018-07-13T11:36:29","modified_gmt":"2018-07-13T15:36:29","slug":"find-call-time-pass-by-ref-php","status":"publish","type":"post","link":"https:\/\/www.devolve.local\/find-call-time-pass-by-ref-php\/","title":{"rendered":"Finding call-time pass by references in PHP."},"content":{"rendered":"

While trying to move an older code base to a newer system and thus a newer version of PHP (5.3 -> 5.5), I knew that some of the code would need to be changed to avoid using some removed features. Specifically, I mean call-time pass by references. For those who don’t know, this is kind of a weird feature of earlier versions of PHP that allows one to call a function and pass any of the arguments by reference rather than the usual call by value if the caller<\/em> prepends an argument variable with the “reference to” operator &<\/code>.<\/p>\n

So, to illustrate, normally this code won’t have side effects because of call by value:<\/p>\n

function do_me( $var ) {\r\n  $var = \"I'm doing it: \" . $var;\r\n  echo $var;\r\n}\r\n<\/pre>\n

However there will be side effects if the caller chooses pass by reference:<\/p>\n

$text = \"happily\";\r\ndo_me( &$text );\r\n<\/pre>\n

I thought a regex might be in order to find these guys and fix them: <\/p>\n

ag -r -G '\\.(php|module|inc|page|view)$' '^(?:(?!function).)*\\([^&]*&[^&#][[:alpha:]$_].*\\)'<\/pre>\n

but it was a naive idea, and this regex devolved (heh) to its current form before I realized I could just use the built-in linter to find the problem spots.<\/p>\n

find . | grep -Fv '\/\\.svn' | grep -E '\\.(php|module|inc|page|view)$' | xargs -n 1 php -l\r\n<\/pre>\n

HTH<\/p>\n","protected":false},"excerpt":{"rendered":"

While trying to move an older code base to a newer system and thus a newer version of PHP (5.3 -> 5.5), I knew that some of the code would need to be changed to avoid using some removed features. Specifically, I mean call-time pass by references. For those who don’t know, this is kind […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[34,41,31],"_links":{"self":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/623"}],"collection":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":1,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/623\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}