I am using Eclipse for PHP. If Eclipse does not have a convenient solution then I am interested in other tools for the solution but hopefully not a different IDE.
When I see a function being called in PHP, what is a convenient way to find where the function is at? For this, I am only asking about functions in PHP files that are not in a class.
I have WordPress installed and working in IIS. So an example of what I am asking about is in the wp-blog-header.php file; it calls the wp function. I know that the wp function is in the includes/functions.php file, I am not asking where it is at. I am asking what is a convenient way to determine that for other functions and files. I know I can look at each file included and every file included by included files until finding the function, but is there a tool that does that automatically or makes it easier or whatever?
I don’t know of any tools or native PHP / WordPress functions that could do this.
I vaguely remember seeing in one of the several IDEs I’ve tried having smething along the lines of what you’re wanting but unfortunately I can’t recall which IDE or even if that is what it did. (Hmm, was it some kind of linter feature ???)
Anyway I think it would be relatively easy to write a script that recursively searched .php files for a regex match of the function name.
If you have a copy of all of the code on your own computer then you can use any one of a long list of search programs to search through the files. My favourite is https://www.fileseek.ca
Usually I have save all files in a project folder and using Sublime I can right-click on the folder, select “Find in folder” and enter “function functionName(”
I am sure Eclipse will have similar functionality.
Let me suggest you to take a look at PHPStorm or NetBeans. Both are exceptionally good with finding the function or method definitions, simply by means of Ctrl+click on the function name.
The reverse functionality is also available, you can choose “Find usage” from the context menu to find the usages of the function you are currently in.
Yes. Before doing that I should determine if something exists as I am doing here. Except my next step would be to look for something that can parse php files (such as Flex (Win flex-bison) if there is nothing more specific) instead of using regex.
Since I have it installed locally and since PHP is interpreted, yes, I have all the source. And I have Microsoft Visual Studio for searching files.
That’s a runtime thing, correct? Perhaps I should have stated explicitly I don’t want a runtime thing.
I did Google first. I also tried right-clicking on the function call. For some reason I missed seeing that in the context menu.
Also, I should say that I tried Doxygen. I don’t know if it will do what I am describing but it has a bug that prevents WordPress from being processed. I have sent a message to the developer about the problem.
Am I missing something here? To me this looks like it’s a feature of any good IDE, I don’t know Eclipse so well but it would be strange if it didn’t have it. In Netbeans you simply choose Navigate > Go to Symbol, type the function name and you’ve got a nice list of functions, methods and classes of the given name. No need to use regex searches or external tools.
[quote=“Lemon_Juice, post:11, topic:235831”]
To me this looks like it’s a feature of any good IDE
[/quote]I am sorry but my previous reply was intended to say that I found it in the Eclipse context menu.
It is my understanding that PHP-Parser parses PHP and produces a parsed version of the PHP. One of the things it produces is the functions defined in the file and something else is the functions called from the PHP file. If a function is defined in the same file that calls it then a caller/called hierarchy exists but if multiple files are involved then we simply process all the files and process all the data together. I am hoping that PHP’s own token_get_all is similar since I can’t figure out how to use PHP-Parser. If I can process all the files and produce tables of the functions and function calls then that will help.
I think you guys are assuming this is easy. It is not. Please don’t assume that something that works for other languages also work for PHP.
Note that the solutions suggested here would be good for small projects but for large projects, automation can save significant amounts of time. The amount of time it takes to do a search is minimal unless the search is done a thousand times; automation of the search can save a significant amount of time if something is done a thousand times.
That page that you reference is for C++. C++ is very different from PHP in this context since C++ #includes are processed by the pre-processor at compile time. Therefore it is easy for an editor to find declarations. Whereas PHP is interpreted and the functions are introduced by an include that is processed during execution and the include can be loaded from a path that cannot be determined prior to execution. So yes, any tool that works in the way I am describing (without execution) would be imprecise.
I cannot find the “Open Declaration” in the context menu. I know I did once, so it probably depends on circumstances.
I will be happy to install NetBeans if it is better than Eclipse for this but I will try to get help from other people also experienced with PHP.
I am very familiar with the Visual Studio IDE. The thing is that PHP is different from other languages.
I assume it’s easy because it’s easy for me and searching for a function is not a difficult task for an IDE and I do it many times every day and it works for large projects very well. Don’t assume something is difficult before trying out all available options
Let’s take your example of looking for the wp function in wordpress. In NetBeans you do it this way:
Place the cursor where the function name is (it does not have to be selected) - or skip this step if you want to type it manually.
Choose Navigate → Go To Symbol or press Ctrl + Shift + Y (I think this is my custom shortcut, not NetBean’s default).
I get a list of all wp functions in the project - NetBeans by default finds all functions and other symbols that contain wp in the name, to narrow down the search to exactly wp add a space at the end. Javascript functions are found as well but I don’t think this is much of a problem.
Scroll down to the file you want and press Enter - no need to scroll if only one is found.
A much faster way is simply click on the wp function in wp-blog-header.php file with Control pressed and you are immediately transported to the PHP function declaration. Alternatively, press Control+B when the cursor is on the function name. What could be simpler and faster than that? I don’t think using external PHP parsers will speed your workflow while NetBeans has its own PHP parser just for this kind of usage.
To some extent you are right but this depends on what kind of PHP code you work with. When you have multiple functions with the same name then yes, the IDE may not be able to tell which one to find - but then I don’t think that’s a big problem because you can simply choose from the list of found functions. Anyway, I don’t think any PHP project written nowadays should have more than one function with the same name since its bad practice, anyway.
However, in modern PHP projects this is not an issue if your code is namespaced and all functionality is enclosed within classes and functions and you are using correct type hints like this:
function myfun(Blog\Entry $entry) {
$entry->delete();
}
Here the IDE will know exactly where the delete() function is because its class has been type hinted - then you simply Control+B and go to the declaration without any mistakes on the IDE part.