views:

73

answers:

3

From time to time, I'm handed bloated, undocumented, buggy PHP code to fix. I have several tools I use to fix such code as quickly as possible.

One thing I'm not sure how to do that occasionally causes me grief is finding where a function is located (in which file it is defined). Note that I don't want to know where a function is called from.

For example I'd like to be able to do this:

//file1.php
function foo(){
    echo 'bar';
}

.

//file2.php
where_is_function('foo');//error
include('file1.php');
echo where_is_function('foo');//outputs'file1.php'
+7  A: 

Get yourself a good IDE such as netbeans or eclipse. They have functionality to find function declarations, and also find the usages of those functions (under refactoring).

Personally I use netbeans. I simply have to ctrl-click on a function name & netbeans will find where the function is defined, and automatically open the file for me.

Ben Rowe
+1 ctrl+click is a very handy feature in netbeans =)
Russell Dias
Let's say I've been tasked by a client with doing a couple fixes to some code they have on a test server. They give me the FTP info and I go in to fix it. I'd find it a hassle to download all the files, and set it up locally with an IDE (which is normally how I would use an IDE - with all the files local/present on my machine). Could I stll use an IDE (preferably netbeans)?
Cam
Netbeans allows you to setup remote projects via ftp for this reason
Ben Rowe
Brilliant. Thanks Ben!
Cam
+1 netbean is great. phpDesigner7 is also good(but costs money)
ggfan
I'm trying to use netbeans, but sadly it won't work - once i get to the final screen for Remote Configuration, the Finish button does nothing (I can press it, but nothing happens). A real shame - it's one of my favorite IDE's. What would be the next best (free) one? Eclipse?
Cam
Update: Was able to get the download working with netbeans 6.8. I'll update my question later with more details of how it's working out :)
Cam
Yeah there's a bug with 6.9 (just tried it myself)
Ben Rowe
+2  A: 

Try this:

function getFunctionLocation($fname) {  
    $includeFilesString = implode(" ", get_included_files());  
    return system("grep 'function $fname' $includeFilesString -l");          
}

But if it's only for development purposes, which it should be, then just run

grep -r "function functionName" *

from the base directory

mwhite
+4  A: 

grep or grep-like tools. I prefer ack, which makes the output much easier to read and ignores version-control files by default. I highly prefer these tools to constraining myself to code in a bloated, click-hogging IDE. Finding things this way has always worked better than things like Eclipse for me anyway.

To use grep, you would cd to the base directory and then recursively grep the contents of the directory for the function name, possibly including the keywords to define a function. Like this:

cd project/
grep -Rn "def wallace(" .

Ack is like:

cd project/
ack "def wallace("
cookiecaper
@Andrey - I upvoted you because you said something funny. You aren't serious, right?
cookiecaper
Vim is my dev environment, so this is exactly how I find functions. No need to be snarky, cookiecaper.
Adam Backstrom