Produce PHP function Call for WordPress - php

I'm using wordpress. I want to view like a backtrace for all the function that wordpress calls. Is this possible with php only or if not, where can i insert debug_backtrace() to view all the function call??

debug_backtrace() (when dumped) will show you the function calls and includes leading to the function within which you call the trace. You can't ask it to do a stack for the whole application (unless the whole application is just one long series of function calls and includes, and in that case you'd still have to find the last step and put the trace call in there).
So for Wordpress, if you're trying to get a feel for the application by trial-and-error, you could try adding the trace in places where you think it is likely.

Related

Stopping execution of an included php file...from a class function

I'm trying to devise a plugin system for a simple web app I'm developing.
Each plugin begins with the function call register_plugin that contains that plugins info, like name, description, etc.
I want to be able to set a mode, say, to 1, and then have the ability to include the plugin file, have it call the register_plugin, and then EXIT the included script only. I know that I can stop execution with a simple return;, however, the register_plugin function is located in a different file, a class, so I can't simply call return because that will only end the function.
How can I do this?
Thanks.
As you already have outlined in your question, you would need to change the structure of your code and files to get this to work.
There is no magic kind of return that would not leave the function but the file or that would leave the function, then the file.
You need to re-arrange your code, add additional checks then to provide the functionality you're looking for.

how to process stored requests in CI

I have been using a controller method post directly to perform some db and social network operations but im finding a few points of failure between it and the hardware — so I came up with the idea of storing all the request in a db table to be used as a queuing system instead so I can process them in my own time rather than real time
The thing I'm struggling with now is handling my requests . I know this isn't very MVC — but its quick fix.
How do I call another controller's method from within my process queue method? I have tried including the file and instantiating it — then passing it the variables i would have done from the web.
function process(){
$result = $this->mque->get_all();
include('post.php');
$get = new post();
foreach($result->result_array() as $item){
$get->index($item['rfid'],$item['station_id'],$item['item']);
}
}
but i get an error- when i call the normal index method- it runs fine but i get an undefined method error when call it through the instantiated class method- (this is my problem)
Message: Undefined property: post::$db
The why
I am setting the process queue method to run based on a cron job running at a set interval of time.
Originally everything ran to index method of post — but since post::index() can take 10-15 seconds to run and the reader is not multi threaded — someone could use the reader within 7 seconds and the script wouldn't have run completely.
Is there a better way of doing this rather than using my current process method?
update
there is two ways to do this- either use php to fopen/get from the web
or do it sprogramming using $class->method()- i would prefer to do this the first method but dont really see any option with the error i mentioned before
That's easy: you don't have one controller call another. As a rule, if you need something to exist in two different places, you have two options:
Have them both subclass the same object
pro: That way the method is already there
con: You can only subclass one thing, and you have to build your own class loading system (NOT GOOD)
Have a library (or model) which they both share
pro: The method can then be tested better (it is (or it was at one point) easier to unit test models than it is to test controllers), the code can be shared without a custom class-loading syntax.
con: This may involve a little refactoring (but it should be as easy as moving the code from the controller's method to a library's method and then simply calling the library in the public controller method).
Either one of those would solve your particular problem. Personally, because of how CI loads controllers, my preference is to create libraries.
CodeIgniter: Load controller within controller
Is this something that could help you out quickly? Check the bottom reply.

Track each function call dynamically

I was wondering, can you make a function or something, that looks for other function calls across the site? I think, that if there is a master page, you could include it at the very top and it should work sitewise.
But I have no idea how to do this.
Kind of like debug_backtrace();, but global. In other words, a function that does something with a function that has been called after this function.
Well, I see that you could do this with a specific call_user_func();, therefore track everything what's going on, but.. any dynamic way?
Thanks in advance!
Goal
The goal of this is to dynamically keep chain of called functions. So at some moment, for example, I require a value back from 3rd function in chain, so I can simply retrieve it with something like $calls[2]; // the stored returned value or an array containing info about function + returned value.
And it adds function data to chain on each call, where 1st call's $key = 0. So for debugging purposes, when my function fails which is x function in a row, I'd love to know information about previously called functions, maybe one has returned wrong value, resulting in error at some part later on.
It looks like you are looking for some AOP technique. You could write the weaved code to return immediately if some (global) flag (show trace) is not set, otherwise print the backtrace.
PHP doesn't support AOP directly but you can either patch the PHP core or use tools to create (transform the) code based on the AOP weavings. I haven't used it for PHP yet, so you have to google.
I don't believe there is, and I doubt if it would work and would be useful. Functions in turn call other functions, even the internal ones. Even the logging functions are functions themselves, so you end up with an enormous amount of information with which you can do virtually nothing relevant. If such a solution exists (it may), it won't be developed in PHP itself, but be a module that is loaded by or compiled in PHP.
If you want to log method calls to class instances, you could make some hook. You can make a class that implements __call. Then, for an instance you want to log, assign that instance to an instance of your log class, and assign that log class instance to the variable in which the original class was stored.
Then, each method call is directed to your class and you can log each call before you call the original method.
Give a try to diyism_trace.php:
http://code.google.com/p/diyism-trace/

Watching function call

Is there a way to trace all calls to a certain function?
The purpose is to plug-in a debug function when "listening" to a function call.
For example, I will say "listen to all calls to mysqli_query()", so I can send the function name (and perhaps the arguments) to a debug/log function.
There is no built-in way to intercept calls to arbitrary functions for logging.
However, the xdebug debugging suite may be able to help you with its execution trace functionality. You'll be able to log both function calls and the arguments involved.

register_shutdown_function() - What is best practice?

I'm currently using register_shutdown_function() for multiple purposes. One use is for handling fatal errors, while the other is for logging resources used during execution like time, memoryusage etc.
Currently I register two different shutdown functions, but on one test only the first ran while the other seemed to fail. Now this could ofcourse be triggered by some error within the function itself so I have rewritten it, but is it possible an error was caused by using several register_shutdown_function calls? So what is considered best practice here, to registert two different functions or just make the call to one function that handles the different tasks?
Is it also safe (and possible) to make the function load a class for errorhandling if a fatal error occurs or should I keep the functionality within the function itself?
The final question I got is if there's a better way to handle fatal errors than using shutdown functions? I tried using set_error_handler, but it does not cover all errortypes so some errors will not trigger this.
Hope these questions are well formulated and clear. My goal is to keep the code as solid as possible and I could not find any decent answers to the questions I had.
*Edit: Found the answer to my first question, registering several functions should be no problem so the error had to be within the function itself. Leaving the question up to get answers to whether there's a better ways to handle fatal errors.
IIRC, if you have multiple shutdown functions registered, they will be executed in the order in which they were registered; and you should never have an exit statement in any, otherwise subsequent shutdown functions will not be run. That means you need to take great care if you have multiple functions rather than a single shutdown function.
However, if you're passing different arguments to the different functions, you should ensure that you have default values for them all in case the functions are called (perhaps triggered by an error) before all the appropriate variables are set.
Personally, I register multiple functions, for similar purposes to yourself; but I'm very careful about the logic within them, and the order of registration.
It's also not a good idea to use includes or similar in shutdown functions (especially where one is an exception handler), in case the include itself triggers an exception

Categories