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.
Related
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.
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.
I have several classes which use static variables.
I assume that the classes are loaded into PHP when I include it in a php file being interpreted. Do I have to actively do anything to make sure that PHP does not 'unload' my class and I lose my static variables? Or does PHP simply never unload Classes once loaded? What if there's no file being interpreted at the current time? (I'm using php-cgi for my webserver)
Once loaded, they are not unloaded until the end of execution. Things do not persist over requests though.
static variable values within a class are set and retained within the context of a request only. If you want data to persist beyond the scope of the request, you have to use a session variable or write to a file or database.
No, all your request scope classes and variables will be available until the request is complete (or a call to a termination method is made, like die() or exit() as Corbin noted in the over answer).
If you use session variables then they will be available during the session is active.
To access a class or function, you simply need to make sure the file is included before you use it.
I'm working on a PHP IRC Bot, and I'm currently working on the commands.
At the beginning of main.php, the script that starts the bot, it includes the class_lib.php file and instantiates the object of that class. I want to work on a !reload command, where it would "uninclude" the file and then reinclude it. Would that be possible, or would it be fine if I just included it again when that command was sent?
EDIT: Basically, I want to be able to modify and reload the class without having to restart the bot.
No, you can't. Revisit your design. Don't couple the definition of the class with the instantiation of the object.
Why not just allow the object to reload the default settings, or restart, instead of what you describe? I'm pretty sure you can't do that anyways.
Also, don't try to load the object with the class_lib.php. Include the class file with the object, then where and when you need it, create your object. That way you can stop it, destroy the object, and then re-instantiate another object, which should accomplish what you want.
Am afraid there is no way to uninclude a file. If it is a function, you could generate a new function dynamically each time.
Check:
http://php.net/manual/en/function.create-function.php
You can assign a function to a variable and then clear that variable and assign it again.
Late to the party, but I have to mention the following PHP-extension:
http://www.php.net/manual/en/book.runkit.php
The runkit extension provides means to modify constants, user-defined functions, and user-defined classes. It also provides for custom superglobal variables and embeddable sub-interpreters via sandboxing.
This would allow you to re-define classes, which happens upon parsing included files.
NOTE: I have not used this, I came upon it after researching if it was possible to somehow hotswap certain classes. I cannot vouch for the safety and can not supply hands-on info. Yet.
Late answer..
You could create an array containing the files and then eval(file_get_contents($filename))
I've been working on my own IRC Client named PITC and that may be the method I might use, It's how i've done stuff before
I'm working on a web application, using the CAKEPHP framework. Herefor i need to request one variable on multiple pages (all pages have different controllers). it is oubvious that i get a error on several pages, since the variable isn't declared in all the different controllers.
Is there a workaround for this? i've already tried the app:: import to import a controller in another controller, but this doens't seem to work (still get a undefined variable error).
Thnx for your cooperation!
Regards,
Simon
Duplicate question, but I think it's phrased a bit better so I'll paste my answer here:
Standing on the shoulders of deceze's comment and DavidYell's answer, I think they've managed to scratch out a decent view of what you're trying to get to. Maybe. So with that loose understanding of what you're seeing and what you have...
By default, the PagesController::display() method generates the homepage view (home.ctp). I suspect that this is what you're talking about. That said, the variable you're setting in a method of your SectionsController won't be available to your homepage which is created by a different method in a different controller. If you want a variable available to all views there are several things you can do:
You can set the variable in your config/core.php file (not generally recommended)
You can set it in config/bootstrap.php if it's a constant. By that, I mean that it's a value you're going to hard code, not something dynamically generated. Whether you create the variable as a constant doesn't matter.
You can set in in your AppController in a beforeFilter() or beforeRender() method. All of your custom controllers (assuming you've followed protocol) inherit from the AppController. If you choose this path, make a copy of cake/libs/controller/app_controller.php and place it in your app/ directory.
Those are the ways that I think will best meet your needs as I understand them.
You can use the Configure.write... more info here
More on configure class
One way to make sure that the variable is available on all pages is to define it on the front controller (normally index.php) or any other always included file (such as global configs), another option might be to use the $_SESSION super global.
You can youse the beforeRender() or beforeFilter() callback methods from your AppController. :)
These'll be called on every page request. :)
If you to access a value from different controllers, you will need to save that value into a database record so it can be accessed by the different controller methods. Each controller call exists in its own context and can only share data that is stored external to the scripts.
In situations like this, I've created a preferences table (with fields like, id, name and value). Then add a $uses value to the app_controller to make it available to all controllers. Finally, just grab it with a find call. (ie. $foo = $this->Preferences->find( 'first', array('conditions'=>array('name'='foo')));