1. Variable Functions :: Object methods can be called using variable names. Check out the below example.
<?php
class a
{
public function disp($str)
{
echo "class a disp() says $str";
}
}
$a = new a; /// Create Object
$new_func = "disp"; /// Assign function name to variable
$a->$new_func("DISPLAY");
?>
Output ::
class a disp() says DISPLAY
Explanation ::
$a->$new_func("DISPLAY") acutally calls $a->disp("DISPLAY"). Static functions can be called using variable functions as shown below.
<?php
class a
{
static function disp()
{
echo "Static FN called";
}
}
$new_func = "disp"; /// Assign function name to variable
a::$new_func(); /// Now call the function using variable
?>
Output ::
Static FN called
Explanation ::
The function call a::$new_func() is converted to a::disp().
2. Some very interesting PHP functions ::
a. If you want to know the list of extensions your PHP is using, write the following statement :
<?php print_r(get_loaded_extensions()); ?>
This would list all the extensions like "Core", "zlib", "json", "xml", "mysql", "zip", "session", "curl", "soap", "ereg" etc.
b. If you want to know the functions available with each extension, run the following statement :
<?php print_r(get_extension_funcs("Core")); ?>
This would list all functions "Core" extension offers, like "strlen", "strcmp", "each", "error_reporting", "func_num_args", "define", "function_exists", "create_function" etc.
<?php print_r(get_extension_funcs("mysql")); ?>
This would list all functions "mysql" extension offers, like "mysql_connect", "mysql_close", "mysql_select_db", "mysql_query", "mysql_error", "mysql_affected_rows", "mysql_insert_id", "mysql_num_rows" etc.
c. If you want to know the list of files included to the current script, run the statement below :
<?php print_r(get_included_files()); ?>
This would show list of all files included to your script with the name of current script. If you do not have any script included, then the name of the current script is shown in the 0th position of the returned array. If other files are included, their names appear from 1st position onwards in the returned array.
d. Always wanted to know about the makers of PHP? Run the statement below ..
<?php phpcredits (); ?>
e. To get the OS info PHP is running on, type the statement below :
<?php
echo php_uname();
echo "----------";
echo PHP_OS;
?>
Output ::
Windows NT TEST-PC 6.1 build 7600 (Windows 7 Business Edition) i586
----------
WINNT
3. Check out one example on PHP closures below ::
<?php
$p = 100;
/// Define the closure
$Hello['all'] = function() use ($p)
{
echo "\$p = $p";
$p++;
};
/// Call the function
$Hello['all']();
/// Print p
echo "\$p = $p";
?>
Output :
$p = 100
$p = 100
Some points need to be noticed in the example above.
a. If we don't use the "use ($p)" to the function definition, the anonymous function does not get access to variable $p in the parent scope.
b. The closure function tries to make changes to $p by calling $p++, but it does not changes $p in the parent scope which is evident when the second "echo" prints value of $p. If we want the closure function to get the true access to the $p in parent scope, we need to use reference in the function definition as shown below :
/// Define the closure
$Hello['all'] = function() use (&$p) //// Reference is used
{
echo "\$p = $p";
$p++;
};
Now, $p is incremented by 1 within the closure and hence, the second "echo" prints "$p = 101".
<?php
class a
{
public function disp($str)
{
echo "class a disp() says $str";
}
}
$a = new a; /// Create Object
$new_func = "disp"; /// Assign function name to variable
$a->$new_func("DISPLAY");
?>
Output ::
class a disp() says DISPLAY
Explanation ::
$a->$new_func("DISPLAY") acutally calls $a->disp("DISPLAY"). Static functions can be called using variable functions as shown below.
<?php
class a
{
static function disp()
{
echo "Static FN called";
}
}
$new_func = "disp"; /// Assign function name to variable
a::$new_func(); /// Now call the function using variable
?>
Output ::
Static FN called
Explanation ::
The function call a::$new_func() is converted to a::disp().
2. Some very interesting PHP functions ::
a. If you want to know the list of extensions your PHP is using, write the following statement :
<?php print_r(get_loaded_extensions()); ?>
This would list all the extensions like "Core", "zlib", "json", "xml", "mysql", "zip", "session", "curl", "soap", "ereg" etc.
b. If you want to know the functions available with each extension, run the following statement :
<?php print_r(get_extension_funcs("Core")); ?>
This would list all functions "Core" extension offers, like "strlen", "strcmp", "each", "error_reporting", "func_num_args", "define", "function_exists", "create_function" etc.
<?php print_r(get_extension_funcs("mysql")); ?>
This would list all functions "mysql" extension offers, like "mysql_connect", "mysql_close", "mysql_select_db", "mysql_query", "mysql_error", "mysql_affected_rows", "mysql_insert_id", "mysql_num_rows" etc.
c. If you want to know the list of files included to the current script, run the statement below :
<?php print_r(get_included_files()); ?>
This would show list of all files included to your script with the name of current script. If you do not have any script included, then the name of the current script is shown in the 0th position of the returned array. If other files are included, their names appear from 1st position onwards in the returned array.
d. Always wanted to know about the makers of PHP? Run the statement below ..
<?php phpcredits (); ?>
e. To get the OS info PHP is running on, type the statement below :
<?php
echo php_uname();
echo "----------";
echo PHP_OS;
?>
Output ::
Windows NT TEST-PC 6.1 build 7600 (Windows 7 Business Edition) i586
----------
WINNT
3. Check out one example on PHP closures below ::
<?php
$p = 100;
/// Define the closure
$Hello['all'] = function() use ($p)
{
echo "\$p = $p";
$p++;
};
/// Call the function
$Hello['all']();
/// Print p
echo "\$p = $p";
?>
Output :
$p = 100
$p = 100
Some points need to be noticed in the example above.
a. If we don't use the "use ($p)" to the function definition, the anonymous function does not get access to variable $p in the parent scope.
b. The closure function tries to make changes to $p by calling $p++, but it does not changes $p in the parent scope which is evident when the second "echo" prints value of $p. If we want the closure function to get the true access to the $p in parent scope, we need to use reference in the function definition as shown below :
/// Define the closure
$Hello['all'] = function() use (&$p) //// Reference is used
{
echo "\$p = $p";
$p++;
};
Now, $p is incremented by 1 within the closure and hence, the second "echo" prints "$p = 101".
No comments:
Post a Comment