How to debug Yii PHP code in NetBeans - php

I have set-up my WAMP installation to debug a PHP website in NetBeans.
This works fine (up to a point, hence this question) and I can single-step through the PHP code, and watch variables, etc.
However, some bits of the code - those provided within the Yii framework - are throwing exceptions, but attempts to single-step through this code fail - the debugger leaps over portions of the code and I can't observe what's going on.
The debugger lands on a call to HandleError, but I can't see how it got there. The displayed Call Stack isn't showing all the steps taken to get where it is.
I've seen lots of comments on the web about this, but haven't yet seen an answer on how to enable this framework's code to be debugged on my PC.
Any suggestions would be welcome.

Related

Compile-time PHP errors in VSCode

Has anyone configured VS Code to show syntax/semantic errors in PHP development? Coming from full Visual Studio/C# world, I really miss this thing a lot. Currently I actually have to call my PHP function using a client, only to get a HTTP 500 error and then check server logs to see what went wrong. This is a huge pain. Going through all of this only to find for example that I didn't import a require namespace, is so frustrating.
I understand that PHP by design cannot detect all kinds of errors like C# at compile time (so to speak), but detecting functions that do not exist or not passing required parameters should be easy to catch.
Is there a plug-in or setting in VS Code that could handle syntax/semantic error in PHP code? I'm using Laravel on the server-side, if that matters.
Looks like I found something. I uninstalled PHP Intellisense extension this morning and installed PHP Intelephense and all seems to be far better. Not only that the syntax and semantic errors are highlighted, it automatically injects required namespaces too. And if that were not enough, Go To Definition (F12) command has started working correctly all of a suden. :)
Must-have extension for any PHP work in VSCode I'd say.

Debug PHP Using WAMP and an IDE

I recently started web development. The course I took was to install WAMP and start developing right away. I used an atom text editor, this -combined with wamp- proved to be a very fast way to write client-side code(HTML, CSS, Javascript).
But when I started to write serverside PHP things got a little messy. I should probably explain my site's structure here.
I keep separate PHP, CSS, javascript files for every page on the client side, for the server side a have 2 different types of PHP files:
Files that only perform a specific operation on the database(For example returning "5 more answers"). These are always called by AJAX requests.
Files that load the page for the first time. These are only used when the user opens the page for the first time, they do necessary database queries and return the page. Later requests always go to the 1st type of PHP files.
Now regarding my problem. I debugged until now by printing variables to the screen with var_dump() or echoing. But this started to become too slow as the data I work with grew. I wonder if there is a way of debugging which will let me but a breakpoint in one of my PHP files. Then, when I open it on the browser, on the localhost I created using WAMP, will let me go through the PHP file step by step.
I have been dealing with this issue for 3 days, I tried to make it work with Eclipse IDE but couldn't find a way. Also, there seems to be no tutorials or Q&A on the internet regarding the issue.
Breakpoint debugging opens a whole new world, and is the natural step after var_dump() debugging. Not only does it speed up development, but it provides much more information about your code, as you can step through each line and see what values have been set at each step, and how they evolve as your program executes its code. This means you can track the entirety of the values at different stages with one run - imagine tracking all variables at each point using var_dump()!
Although choosing an IDE is a personal decision based on personal taste, i strongly recommend you try out PhpStorm. If you can get a student licence go for it.
PhpStorm has extensive documentation & tutorials on all features in the IDE, debugging is no exception:
https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html
https://www.youtube.com/watch?v=GokeXqI93x8
I don't know of a specific solution to your issue. I'm not exactly sure what you're doing but as a quick tip, I find add the following snippet to the top of the file useful as it will highly error more easily rather than browser just say nope.
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Hope this help you a bit.
I tried out what's recommended in comments and answers. I first tried Netbeans. To be fair it disappointed me. Download kept getting stuck at 100%, even for different versions. When I stopped downloading and went ahead to create a php project, there was missing parts I guess. I couldn't even manage to create a php project. But that might just be me not being able to do it.
Then I followed #leuquim's answer and #Alex Howansky's comment and downloaded PHPStorm. And I got it to work in no more than 20 minutes. I downloaded it with a student's licence. For people who want to use PHPStorm with WAMP here's a Youtube tutorial:
https://www.youtube.com/watch?v=CxX4vnZFbZU
One thing to note in the video is that, maker of the video chooses PHP Web Application in the Run Configurations. That has been changed to PHP Web Page.

Eclipse PHP Neon.2 with Subversive produces project connection error after committing a PHP-configured project

In testing Neon and the new Subversive version's interaction with existing workspace projects prior to migrating my team's IDE installations, I've come upon a curious issue that did not take place using Mars.1 with Subversive 3.0.2 and Native JavaHL 1.8.1.
I've found that when a project has been "configured as a PHP project", any non-empty ('may apply to empty ones as well) commit results in one or more of a startling-looking error, one that causes the project to disconnect from source control.
(The commit meanwhile, goes through to the server just fine.)
That can be easily remedied via the team=>share project command, but I'm interested in stopping the problem from taking place.
The error turns up in the log as a failure of "Get Resource List" to connect to the project, followed by a cascade of other SVN-related tasks failing for the same reason, until the logger gives up out of enui and says "... 9 more".
Killing the build commands org.eclipse.wst.validation.validationbuilder and org.eclipse.dltk.core.scriptbuilder, as well as the org.eclipse.php.core.PHPNature nature in the .project file to make it act like a fresh unadulterated project prevents the issue, but naturally that's not an acceptable solution either, since that disables the nice DLTK toys, like occurance highlighting.
I actually made a post on the eclipse forums about the issue, and learned much of the above while engaging in continued investigation, including a lot of failures to replicate until hitting on the .project settings.
Admittedly I didn't find anything recent that was really like this on their bugzilla, so I may just have to submit a bug report on this, but are there any known issues involving negative interaction between Subversive and one of those modules? And/or any known work-arounds that aren't as dumb as "tell them to ignore the errors and manually re-connect" or "don't configure as PHP"?
To be complete about environment, I'm using Eclipse PHP Neon.2 with Subversive 4.0.2 using Native JavaHL 1.8.15, all on good ol' 64b win 7.
Simply switching to the SVNKit connector has sufficed, it turns out.
No telling if there were or not known issues, even after my combing through their bugzilla, but, experimentation resulted in several issue-free commits while configured as PHP using the SVNKit connector instead (SVNKit 1.8.14), so the issue could well be in JavaHL, either 1.8.15 specifically or any range of versions from 1.8.15 back to 1.8.2 at the oldest.

How to debug a PHP script that never finishes loading?

I have been tasked with setting up a website on various environments for different stages of evaluation (dev/test/staging/etc).
On our staging environment however, it seems there is some difference preventing the PHP script from finishing, so the page is never delivered to the browser.
I'm wondering if there is a way I can output to log some sort of stack trace or backtrace upon cutting the connection, or is there some other method to find out what exactly PHP is doing at any given point in the script's life cycle?
It's a Drupal site, so it involves a lot of code I'm not familiar with, and could take hours to sprinkle die; commands throughout to see where the script is loading to.
I understand I should probably be looking at the differences in environments, however all should have very similar configuration (Ubuntu 11.04) and the staging environment seems entirely happy to serve other PHP sites whilst this particular site is refusing to finish. If anything this staging site has more resources available that other environments which are not having problems.
UPDATE: Sorry all, found the problem in the end. The staging environment was on a VLAN that was not permitted to access itself via public IP, and for whatever reason (still confused about this) it was trying to access itself as part of the page load and never completing the request. Setting a hosts file entry for 127.0.0.1 fixed the issue.
Debugging an issue like this step-by-step using a tool like xDebug is an option, but will probably take a long time -- finding where to put the breakpoints is going to be on about the same level as working out where to put die statements around the code. The debugger option is a better way of doing it, but won't save much in comparison, when you have a problem like this where you have an unknown blocker somewhere in large amounts of unknown code.
But xDebug also has a profiler tool which can show you what functions were called during the program run, how long they took, and highlight where the bottlenecks are. This will probably be a better place to start. Just configure xDebug to generate a profiler trace, and then use kCacheGrind to view the trace in a graphical environment.
If your program is getting stuck in a loop or something specific is taking a long time to complete, this will pinpoint the problem almost straight away; you'll be able to see exactly which function is taking the time, and what the call chain looks like to get to it.
It's quite possible that once you've seen that, you'll be able to find the problem just by looking at the relevant code. But if you can't, you can then use xDebug's step-thru debugger to analyse the function as it runs and see what the variables are set to to see why it's looping.
xDebug can be found here: http://www.xdebug.org/
Use xDebug.
Its very easy to install and use.
it has few options like breakpoints and step by step to track status of PHP script before finishes loading
and you can download xDebug from here http://www.xdebug.org/
step by step tutoril for set up xdebug is availble at sachithsays.blogspot.com/

How to do Debugging of PHP?

how we do basic debugging in PHP ?
Can anybody share true horror story on debugging PHP application (or (even better) on PHP framework such Codeigniter and Wordpress) ?
i love to hear real experience in case i have to encounter similar situation on my journey to learn PHP.
XDebug may be used to debug a PHP application. Basically you install this on your server and configure it in PHP.ini. Then on your desktop machine, you can setup Netbeans (and maybe Eclipse, but I have not tried that) to remote debug. When you are in a remote debug session you can do any of the things you typically expect: set breakpoints, step through code, etc.
One major annoyance when using CodeIgniter with mod_rewrite is that the debugger gets confused by index.php not showing up in the URL. I am not sure if this is a problem on the client side or server, and whether CI "eating" the GET portion of a URL has anything to do with it. In any case, I have worked through it by copying a 'debug.php' file to the server and starting debugging with that file - everything seems to work fine after that.
I any case, I highly recommend you get setup with XDebug at some point. You may not use it everyday, but when you need it you'll be thankful you did your homework ahead of time.
Easiest option (without installing any IDE) is to use firefox FirePHP addon.
Download and install FirePHP firefox addon
Download FirePHP core library from their website
Extract FirePHP library you downloaded in step 2 and only copy FirePHP.class.php to CodeIgniter /application/libraries/ folder, but give it a name FirePHP.php without the class part
Turn PHP’s output buffering on
Start your Firefox, click the FireBug icon on the bottom lower area, or go to menu and select tools. After you turn on (set them to enable) FireBug and FirePHP, be sure to click on the Net tab next in line to HTML, CSS, Script, DOM and set it also to enable.
See this link for how to use firephp library: http://speedtech.it/2009/05/debugging-a-codeigniter-application-with-firephp/
There is one problem in using IDE debugers - it's bad for debuging AJAX applications. In this case you will need to use some of browser extension. For FireFox it's FirePHP, and gor Google Chrome it's Php console.
I checked out a lot of debuggers 2 years ago when I wanted to set up a debugging environment for myself in PHP. I posted a brief post on this, you can go through it http://forums.codewalkers.com/general-chat-93/which-php-ide-to-use-840352.html
Bottomline was that Nusphere's phped was the best and its debugbreak() function makes debugging a real bliss. With codeigniter also this works fine, no tweaks required if you have already enabled get variable. The only hiccup with codeigniter+phed is that, you cannot do code navigation like "right click and find declaration" for other files models.
Off late I have been checking out codelobster they have a codeigniter plugin, but I am not through with it yet.

Categories