Is there a short solution to avoid the execution of the "exit" language construct? Otherwise I'll have to execute my function with another PHP-request.
Sample: main.php
echo 'hello world';
doSomething();
updateSomething();
Sample: doSomething.php
function doSomething()
{
$a = 1+1;
exit;
}
Sample: updateSomething.php
function updateSomething()
{
$b = 3+5;
exit;
}
But the second function will never be executed ...is there a simple solution? I've found two existing topics, without a real solution. But maybe there is smart trick for my example?
It's not a real example, in my case the functions represents some complex methods, which are called with ajax reqests without a return statement. Because the method are from a external library, I can't modify the code. And I want to call two methods of this library in one single script, if it's possible :)
And ... I know what return is doing, but in my case I can't modify the code, because it's not my code - it's code from an existing software and I just want to call these existing methods in a custom script. But I can just call one of the methods because of the exit-function in every method and it would be great to call them multiple times.
It's not possible to skip or disable an "exit" command.
If you want to do something before the script stops, you can use shutdown-functions or object-destructors ... but I don't think that's something you're up to, is it?
http://www.php.net/manual/en/function.exit.php
Simple. Remove exit; from the functions.
exit() will terminate execution from the script. You probably want to use return which returns back to the calling code.
The year is 2020 and a way to do what question author wanted to now exists - you can install uopz extension from PECL (version 5.0.2 or above), and use uopz_allow_exit() function to disable exit()'s, like this:
echo 'hello world';
uopz_allow_exit( false );
doSomething();
updateSomething();
uopz_allow_exit( true );
Note, however, that you will need to add this config option into your php.ini file:
uopz.exit = 1
If you dont, then all exit() calls will be disabled by default everywhere unless explicitly enabled by uopz_allow_exit().
It is also worth noting that this config option appeared only in version 6.0.1, which, for me at least, failed to install on PHP 7.0, and worked only on 7.2
Oh, and according to the documentation this extension is supposed to be used for unit testing, not production. Then again, nothing in it says that using this extension in production is discouraged, so it's your call.
From the manual:
Terminates execution of the script.
exit (alias of die) is the final thing the script does. Switch to an alternative use
function doSomething()
{
$a = 1+1;
return true;
}
Related
Is there a way to know the value passed to exit() from within a shutdown function?
For example:
<?php
function handleShutdown(){
// Is there a way to know the value passed to exit() at this point?
someCleanup();
}
register_shutdown_function('handleShutdown');
if(somethingWentWrong()){
exit(3);
}
exit(0);
This is in the context of a complex set of legacy scripts running in CLI mode. I am looking for a solution to instrument their exit status within php that does not involve modifying all the points where the scripts invoke exit() (so, not using a global variable, define or forking the whole script).
There is this other SO question asking to know whether the exit was is "clean", but it does not address the exit code at all.
You can install uopz, which will give you uopz_get_exit_status().
Then use it in your handleShutdown function:
$exitCode = uopz_get_exit_status();
Suppose I do
require('lol.php');
whereby lol.php contains the following function declaration
function lolfunc(){
}
is it possible to "unrequire" lol.php such that I can then require another file
require('lol2.php');
whereby lol2.php contains a function with the same name previously declared in lol.php:
function lolfunc(){
echo "this is lol2 biyotch";
}
and have lolfunc() be the one declared in lol2.php? eg if I call lolfunc() it'll echo "this is lol2 biyotch"??
My answer: don't do that.
Try to work with the original author to include the functions you need into a patched version of the old code and then use that patched version everywhere.
If you can't do that, find out how big a job it would actually be to update all the code to new version. Start with white-box analysis: see what's changed in terms of interfaces, data structures et al. Then examine the calling code to see whether the caller cares about any of the things that have changed.
If you can't even do that, use namespacing or some other form of wrapping so that you can include both libs. However, make sure any initialisaton or setup is done on both libs!
What you should do, is to include the required (sets) of files based on conditional clauses, instead of trying to "unrequire".
So:
if($flag_use_oldver)
{
include("oldver.php");
}
else
{
include("newver.php");
}
If you want a more sophisticated solution, of course you could try to have a wrapper class hierarchy that will extend/override as required, but I think that is a bit over-engineering for a pretty straightforward problem statement.
in php i wrote my own debug function which have two arguments: text and a level of message. However i could be also you the php functions for triggering errors. But to debug in development i use sometimes like this:
debug($xmlobject->asXML(),MY_CONSTANT);
now i want to know whether it is a lack of performance in non debug executing because the arguments are calculated indepent whether they will be used inside function? and how to do that right that is only calculated if i need?
Thanks for your help,
Robert
If you write the following portion of code :
debug($xmlobject->asXML(),MY_CONSTANT);
Then, no matter what the debug() function does, $xmlobject->asXML() will be called and executed.
If you do not want that expression to be evaluated, you must not call it; I see two possible solutions :
Remove the useless-in-production calls to the debug() function, not leaving any debugging code in your source files,
Or make sure they are only executed when needed.
In the second case, a possibility would be to define a constant to configure whether or not you are in debug-mode, and, then, only call debug() when needed :
if (DEBUG) {
debug($xmlobject->asXML(),MY_CONSTANT);
}
Of course, the makes writting debbuging-code a bit harder... and there is a bit of performance-impact (but far smaller than executing the actual code for nothing).
The arguments are sended by value, ergo the method ->asXML() is executed always.
I'm trying to reduce the warnings that are sent to my apache server log.
One warning is:
Call-time pass-by-reference has been deprecated.
It is hard for me to imagine why this was deprecated since it is such a useful programming feature, basically I do this:
public function takeScriptsWithMarker(&$lines, $marker) {
...
}
and I call this function repeatedly getting results back from it and processing them but also letting the array $lines build up by being sent into this method repeatedly.
To reprogram this would be extensive.
I don't want to just "turn off warnings" since I want to see other warnings.
So, as call-by-reference is deprecated, what is the "accepted way" to attain the functionality of this pattern: namely of sending an array of strings into a method, have them be changed by the method, then continuing to use that array?
Actually, there's no problem with the way you define the function. Is a problem with the way you call the function. So for your example, instead of calling it like:
takeScriptsWithMarker(&$lines, $marker);
You'd call it like:
takeScriptsWithMarker($lines, $marker); // no ampersands :)
So the feature is still available. But I don't know the reason behind this change.
like noted above in a previous answer, the issue is at CALL time, not definition time.. so you could define a function as:
function foo(&$var1,$var2,$var3=null){
// procesing here
}
then call as:
$return = foo($invar1,$invar2);
your first invar is passed by reference, second one is not.
the error appears when you try to call like so:
$return = foo(&$invar1,$invar2);
You can set allow_call_time_pass_reference to true in your php.ini file. But it's a hack.
You could pass an array with a reference in:
public function takeScriptsWithMarker(array(&$lines, $marker))
which should only take a small amount of refactoring at the other end.
You could pass in the array, let it manipulate it, and then "return" it, instead of messing with the original reference. It shouldn't be too hard to just include a return and assignment.
public function takeScriptsWithMarker($lines, $marker) {
//...
return $lines;
}
Usage:
$lines = takeScriptsWithMarker($lines, $marker);
How can I detect, using php, if the machine has oracle (oci8 and/or pdo_oci) installed?
I'm working on a PHP project where some developers, such as myself, have it installed, but there's little need for the themers to have it. How can I write a quick function to use in the code so that my themers are able to work on the look of the site without having it crash on them?
if the oci extension isn't installed, then you'll get a fatal error with farside.myopenid.com's answer, you can use function_exists('oci_connect') or extension_loaded('oci8') (or whatever the extension's actually called)
The folks here have pieces of the solution, but let's roll it all into one solution.
For just a single instance of an oracle function, testing with function_exists() is good enough; but if the code is sprinkled throughout to OCI calls, it's going to be a huge pain in the ass to wrap every one in a function_exists() test.
Therefore, I think the simplest solution would be to create a file called nodatabase.php that might look something like this:
<?php
// nodatabase.php
// explicitly override database functions with empty stubs. Only include this file
// when you want to run the code without an actual database backend. Any database-
// related functions used in the codebase must be included below.
function oci_connect($user, $password, $db = '', $charset='UTF-8', $session_mode=null)
{
}
function oci_execute($statement, $mode=0)
{
}
// and so on...
Then, conditionally include this file if a global (say, THEME_TESTING) is defined just ahead of where the database code is called. Such an include might look like this:
// define("THEME_TESTING", true) // uncomment this line to disable database usage
if( defined(THEME_TESTING) )
include('nodatabase.php'); // override oracle API with stub functions for the artists.
Now, when you hand the project over to the artists, they simply need to make that one modification and they're good to go.
I dont know if I fully understand your question but a simple way would be to do this:
<?php
$connection = oci_connect('username', 'password', 'table');
if (!$connection) {
// no OCI connection.
}
?>
As mentioned above by Greg, programmatically you can use the function_exists() method. Don't forget you can also use the following to see all the environment specifics with your PHP install using the following:
<?php
phpinfo();
?>