Is there a way to get internal data from php? - php

I know there are PHP debugging tools available, but I'm curious about doing something like this myself.
Is there a way to obtain the data that is being processed by PHP for debugging purposes? For example, without having to change the code of my PHP application, is there some way, when I run a function, I can see what variables exist within that function, what called that function, what the return value was etc?
The solution doesn't specifically doesn't need to be in PHP, ie this could be somethig that is written in C etc

Well, may I suggest you look into adding firePHP to your code. FirePHP has 2 components, a server side component, and a browser component for firefox, it uses the firebug addon. Once installed properly you can do things like FB::Log($variable) and you will see this information inside of the log portion of firebug, when you click on it, it shows the entire variable broken out. Also if you were to install the error handler, when you get a caught exception, you are able to see the entire stack trace of how this exception was invoked.
Highly recommended.

try PHP xdebug module - http://xdebug.org/
u can log the debugging info,
or output as HTML
changes might required to initiate xdebug

You can use some introspective functions like debug_backtrace, but these will only get you so far. To gather any kind of information, you would need to hook into PHP itself, which you would most likely do with an extension written in C. I'd suggest you check out one of the existing debuggers to see how they do it, for example xdebug.

Related

Debugging PHP Code with debug_backtrace

I love to save time using someone else's code. And I want to start effectively debugging my scripts, as well as scripts I inherit from other developers.
I've been reading up on debug_backtrace(), and I'm not sure if it's what I'm looking for.
Basically,when a class is instantiated I want to know what methods are being fired.
Truthfully, I'd like to know as much as possible, but knowing what's going on inside a single class would be fantastic.
<?php
require('aHugeComplicatedClass.php'); // sooooo many methods
try {
$obj = new aHugeComplicatedClass($params);
}
catch(Exception $ex){
var_dump($ex);
}
?>
From PHP's docs about debug_backtrace, it looks like I need to place the debug_backtrace() function inside each method/function within any and all classes, just to see how it was reached.
I gotta be reading this too literal. That would be a ton of modifications.
So, if I have a php file, that instantiates a class, and I know this class is extended from other classes, what's the simpliest way to debug that Object?
I would install XDebug and hook up the remote debugging to your IDE (e.g PhpStorm or Eclipse), that way you will get nice stack dumps on all errors, plus the ability to breakpoint your code and inspect the stack and all object internals at your leisure.
http://xdebug.org/
You can also use it to profile your application call chains without making any code changes (which sounds more like what you are wanting). By using the profiling options, which generate big log files, you can then load these logs into webgrind and visually inspect who's calling what in nice tree structures.
https://code.google.com/p/webgrind/
The Zend tool chain would also provide this kind of deeper debugging functionality out of the box.
Alternatively install an Application Performance Monitoring agent such as App Dynamics or New Relic for similar code-profiling. This is most useful for remote installations (i.e. production) where debugging isn't an option and profiling is expensive.
We use NuSphere PhpED for getting all such things. It can trigger debugger to stop on specified exceptions and/or errors and shows complete call-stack that may include php functions calls, php method calls, embedded functions calls and embedded method calls.
http://www.nusphere.com/products/phped.htm
I was told in the beginning that their debugger is the best and can confirm this. It stems from the OSS project
http://sourceforge.net/projects/dbg2/
With PhpED IDE we run full cycle of development -- coding, debugging, profiling, testing and uploading to the production server.

Any PHP debugger, Chrome or Firefox extension, to know which session variable setting where?

Does anybody know how can I debug to check where my session variable is setting in the code. There are lots on included files too. I tried site wide search for that session variable but unable to find any clue where it is being set.
May be anybody know any chrome or firefox extension that tell which variable setting where in the code. Just like firebug for javascript, where we can use debugger to check all the above possibilities.
The correct answer to your question is "Use a proper PHP debugger, like xDebug. This will allow you to do lots of stuff, including examining variables at any given point in the program, stepping through line by line, etc. Combined with a good quality IDE like Netbeans or Eclipse, it's an amazingly powerful tool.
However, if you really want a browser plugin, you could try either FirePHP or Chrome Logger.
Both of these will require you to put debugging code into your program that sends debug data to the browser, but the debug info appears in the dev tools, rather than messing up your rendered page output, so it's a lot cleaner to use than just using echo or print_r, etc to display the info.
Hope that helps.
$_SESSION PHP variables are set server-side; you won't be able to access them in the browser unless you output them there.
I would suggest placing several var_dump($_SESSION) statements in your PHP code to debug what key/value pairs are living in the session as your script executes.
At the point in your php script you wish to know what is stored do this:
echo "<pre>";
var_dump($_SESSION);
echo "</pre>";
exit;
You can use php debugger Like (XDebug or Zend Debuger) by adding watch command which will track all changes of that variable, you will need capable IDE for that though.

Using Eclipse to see where a function was called or class used

I am using Eclipse with PHP and I am trying to trace back the calls functions and instantiations of classes to see why a certain piece of code was used.
The Eclipse interface is a bit cluttered. How do I see the function call trace? Is that possible to do?
Thanks,
Alex
A possibily good solution would be to use a debugger, in step-by-step mode -- or by setting a breakpoint where you want to see the call-trace.
This will allow you to see variables and the call trace / history -- and, also, to go line by line into your source-code's execution.
About debugging and Eclipse, the following question + answers should be able to help : Good Free PHP debugger?
This post of me can't be actually a solution but just wanted to share.
In addition to what's already answered (which enables call trace during runtime), PHP plugin of Eclipse called PDT has a feature called Call Hierarchy. However, it looks like this feature has not been implemented as of the latest version 3.1.
This is why I said my post isn't an answer, but I'm hoping that spreading the words like this would motivate the PDT enter code hereplugin developers.
What you probably want is the Parameter stack, found in Show view under PHP Tools and Parameter stack.

FireBug extension to examine PHP variables

We have FireSymfony that allows one to view the Symfony, PHP variables in a firebug panel, so I wonder whether there is a similar extension that allows me to view PHP variables for general PHP apps ( not just Symfony apps)?
You may want to check out FirePHP. It is an extension to FireBug that allows sort of an extended logging of PHP through the Firefox Browser/Firebug Console.
Probably somewhat close to what you are looking for, even though you'd still have to make specific use of it in the PHP scripts.
I think that is part of what Symfony supports, exporting PHP variables to client side for debugging.
you see, PHP variables are located on the server, not at the client. Thus when you load the page, you won't be able to see the PHP variables from client side.
Thus if you want to debug PHP variables, you must have a way for your application to export the variables out to the client side for firebug or any other extension to debug.
In other words, it's not really possible for that to happen for ALL php applications.
Another option is Formaldehyde which integrates nicely with Firebug.

PHP Console that doesn't require heavy installation or a desktop app?

I'm looking for clean way to break my current habit of using print commands in PHP when I want to see what's happening.
I am aware of options such as Zend Debugger but I use Coda for development and I'm not interested in mixing other software or having to do server commands. I just need a console that can be added to my codebase and then turned on/off.
Does anything like this exist? Furthermore, what do you use and why?
EDIT: There was a lot of stuff out there but I needed something even simpler so I ended up coding it myself. It didn't take long (nor is it very pretty) but I've put it up on my server for anyone else interested.
There is very good extension for Google Chrome - PHP Console.
You can use Xdebug in combination with any one of the many options available to view its debugging info.
There's FirePHP which will write your PHP log messages to the Firebug console.
I am aware of options such as Zend Debugger but I use Coda for development and I'm not interested in mixing other software or having to do server commands. I just need a console that can be added to my codebase and then turned on/off.
Not entirely sure if I understand you there, but couldn't you log to a file and then have a console window running with tail -f /path/to/log-file.txt? That works pretty well for me. Of course, you do need a console connection to the server for this.
Using Coda? That means you are using a mac. You probably want to look at XDebug and http://www.bluestatic.org/software/macgdbp/
There is another extension for Chrome available called ChromePHP. It's a little simpler than some of the other solutions:
https://chrome.google.com/webstore/detail/noaneddfkdjfnfdakjjmocngnfkfehhd
Getting started guide available at:
http://www.chromephp.com
During my development career i have not stumbled upon a software that displays the output you describe in your question. It would be a wonderful program or feature i believe but sadly i have not found any.
Like Andrew and nickf have answered, there are some good tools out there and until someone sits down and writes this, it looks like we are out in the cold.
I was looking for similar solution which wasn't heavy and even better I didn't need to include any classes, so I created simple function that outputs php to browser console, you can check it here:
Outputting PHP To Browser Console

Categories