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

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.

Related

PHP uninclude or reinclude file in interactive mode

I'm testing some database related functions in interactive mode.
The first thing I did is to include the testing file, let's say database.php
Then I can make change to the database by a function call.
The question is, when I make any changes to database.php, I have exit PHP interactive mode, re-enter, include the testing file again.
I'm seeking a way to reload the include file during the interactive mode.
There is no simple method of doing this cause PHP is not built for this job, but there are some things you can take a look at as it might do the job for you. However this all depends on what is in your database.php.
Create a simple function like reset and use PHP's runkit functions to update your include.
If your database.php contains functions, you need to remove the functions before including it again. If your file has a class defined in it you could try the import function and just call the function that does all this for you but in the end this is all manual labor and it might be simpler to look at other alternatives.
I for one use a auto refresh timer in my browser to refresh the page every # seconds. However I have two screens which makes using this method much easier.
That is something you should never do. It will create double functions, which will create confusion in the PhP interpreter.
You should require files out of your scope, so they are globally available, That way you can reduce the server overhead (memory usage) and reuse the included class directly without requiring it again.
Or you could create an autoloader, which imports the file when needed. If it is already there, it will return the needed instance without the extra overhead. An autoloader keeps track of the already included or required files.
That said, with include or required, you could load files. Instead of required_once or include_once, they keep including files.

Make certain variables hidden inside PHP file

I was wondering if it is possible to hide variables from the "include". In other words, I want certain classes/variables which are declared inside a PHP file to basically not be visible to any other PHP file which includes it. Is this possible? Is there maybe a way around it?
Method 1: Separate those variable/class into yet another file, and call only the needed part of it. If you are going to use different part of the scripts in different context, place them in separate files and call them as you need it.
Method 2: After you include the file, unset the variables and destroy the classes.
IMHO, I think it's about time you get an introduction to PHP5 OOP.
Take a look at Visibility afterwards.
Your include file could define a function that does all the work, and the variables can be local to that function. As long as you don't declare them global they'll be gone when the function returns.
This function name will be visible to the caller, but it's better than having everything visible.

Produce PHP function Call for WordPress

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.

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/

how to make a project that supports plugins

I have been asked to do a project where the client can create PHP plugins and add them to the website… same concept as WordPress plugins.
What is the best way to make a software that supports that? I'm not asking for the solution, I'm asking for the concept or structure.
First of all, you have to determine how do you want your plugins integrated. If you want something like Wordpress plugins, which are just plain functions most of the times, then it's extremely easy. It's like adding new code to your application.
Well, basically, your application should be able to build a list of plugins available, and to determine which ones are enabled. The list can be built by browsing the files, a config file, or whatever. Enabled plugins can be stored in the database for example.
Then, you just do an interface where the user can enable each available plugin. When enabling it, the web application would write information about it in the plugins database, specifying that it's enabled. Then, the application calls a specific method of the plugin class (really, OOP is mandatory in correct plugin development), like Install(). To avoid exceptions when plugins have no such method, all plugin classes should inherit a base class written in the main web application, which does contain a definition for that method. That method has the sole purpose to perform the initialization of the plugin (create tables to the database, populate statistics, etc).
Also, at each request, your web application should include all the files of the plugins that are specified as enabled. Then use them however you want. Call their methods from templates for example. If you want to write plugins that are transparent to the template developer, then you could do just like in wordpress. There, the plugin at initialization (for example when it's file is included) writes specific data into arrays of actions. It can write what function should be called, with what parameters, and when to be called.
You can even make something like event triggers in your main web application, to enable customization. Before drawing something, call a function called something like do_action("before_output_of_something"), and after output is ended, call the function do_action("after_output_of_something"). The string parameter would be a key in an array of triggers, that points to an array of actions (event handlers). To add event handlers call a function like this add_action("before_output_of_something", $object, "method_name", array("parameters")). do_action method passes the array of actions, and uses call_user_func() PHP function to call event handlers.
There is much more to this topic. All depends on what exactly you want to make.
What I wrote here is just a lousy attempt to express some of the possibilities. What you should do is find a good book, maybe about just this topic.
I would solve this by using the event-dispatcher pattern. You define certain points (events) in your main application. Plugins can register for this events and will be executed, when the related event is triggered.
There are multiple options. Most common is the hooks concept with simple plugin functions, where you just have to include() a plugin from a central configuration script. Each plugin has to register itself, so usually there is a plugin registration method. But it can also just look like this:
$app_plugin["add_title"][] = "do_whatever";
function do_whatever($request, &$page) {...}
And in your main application you just define a couple of plugin calls, wherever you see the need for it. You would use a wrapper function to invoke all plugins, but basically you look for the hook name and invoke all registered plugins:
foreach ($app_plugin["add_title"] as $func) {
$output .= $func($_REQUEST, $page);
Each named hook can have a different set of parameters. Typically you would want to get additional content, but sometimes plugins just modify existing variables, or influence the application flow.
The more hooks you have, the more diverse extensions are possible. And it's not necessary to use functions only. Of course this scheme also works with objects (they just need to be instantiated first.)

Categories