Tag Archives: function

Named Arguments

Named arguments is a new feature introduced in PHP 8.0, which allows you to pass arguments to a function or method by specifying the parameter name along with the value. This feature provides developers with more flexibility and improves code readability.

Traditionally, in PHP, function parameters are passed by position, which means the order in which arguments are passed to a function must match the order of the parameters in the function signature. In contrast, named arguments allow you to specify which parameter you are passing the argument to, without being constrained by the order.

call_user_func_array

call_user_func_array — Call a callback with an array of parameters

Calls the callback given by the first parameter with the parameters in param_arr.

Returns the return value of the callback, or FALSE on error.

<?php
 class Test {
 public function __call($name, $args){
 call_user_func_array(array('static', "test$name"), $args);
 }
 public function testS($l) {
 echo "$l,";
 }
 }
 
 class Test2 extends Test {
 public function testS($l) {
 echo "$l,$l,";
 }
 }
 
 
 $test = new Test2();
 $test->S('A');

?>

stream_copy_to_stream()

int stream_copy_to_stream ( resource source, resource dest [, int maxlength [, int offset]] )

Makes a copy of up to maxlength bytes of data from the current position (or from the offset position, if specified) in source to dest. If maxlength is not specified, all remaining content in source will be copied.

stream_copy_to_stream($source_file, $destination_file);

escapeshellcmd

escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.

Following characters are preceded by a backslash: &#;`|*?~<>^()[]{}$\, \x0A and \xFF. ‘ and ” are escaped only if they are not paired. In Windows, all these characters plus % and ! are replaced by a space instead.

<?php
 $command = './configure '.$_POST['configure_options'];
 $escaped_command = escapeshellcmd($command);
 system($escaped_command);
 ?>

session_regenerate_id

session_regenerate_id — Update the current session id with a newly generated one

session_regenerate_id() will replace the current session id with a new one, and keep the current session information.

When session.use_trans_sid is enabled, output must be started after session_regenerate_id() call. Otherwise, old session ID is used.

Example

<?php
 session_start();

if (isset($_SESSION['destroyed'])
 && $_SESSION['destroyed'] < time() - 300) {
 remove_all_authentication_flag_from_active_sessions($_SESSION['userid']);
 throw(new DestroyedSessionAccessException);
 }

$old_sessionid = session_id();
 $_SESSION['destroyed'] = time(); 
 session_regenerate_id();
 unset($_SESSION['destroyed']);
 $new_sessionid = session_id();
 echo "Old Session: $old_sessionid<br />";
 echo "New Session: $new_sessionid<br />";

print_r($_SESSION);
?>

is_soap_fault

This function is useful to check if the SOAP call failed, but without using exceptions. To use it, create a SoapClient object with the exceptions option set to zero or FALSE. In this case, the SOAP method will return a special SoapFault object which encapsulates the fault details (faultcode, faultstring, faultactor and faultdetails).

If exceptions is not set then SOAP call will throw an exception on error. is_soap_fault() checks if the given parameter is a SoapFault object.

use_soap_error_handler

This function sets whether or not to use the SOAP error handler in the SOAP server. It will return the previous value. If set to TRUE, details of errors in a SoapServer application will be sent to the client as a SOAP fault message. If FALSE, the standard PHP error handler is used. The default is to send error to the client as SOAP fault message.