PHP language improvement suggestion
| From: | Dor | Date: | Tue, 17 Mar 2009 21:05:10 +0000 |
| Subject: | PHP language improvement suggestion | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-4097@lists.php.net to get a copy of this message | ||
Hi,
I have an idea that I would like to share with you.
Sometimes a PHP programmer wants to "wrap" a code snippets with a specific
code/procedure.
This would make the code block to go through a specific procedure before
(optional) and/or after (optional) the code block execution.
It will be implemented as the following:
/* PHP code */
function my_wrap_function($time, $str1, $str2 )
{
echo 'This is the ' . $time . ' time you entered this page.';
update_session_data_in_database();
__run_wrapped();
echo $str1 . $str2;
}
$callback = 'my_wrap_function';
$params = array(++$_SESSION['times_entered_the_page'], 'World', '!');
wrap($callback, $arguments)
{
// here will be the code that I want to wrap with some procedure
// example:
run_some_function();
$foo = 'Hello';
echo $foo;
}
/* End of PHP code */
The "wrap" is a control structure, which redirects PHP to execute another
code (i.e. the callback) and passes some arguments to the callback.
In the callback function, the wrapped content would be executed with the
appearance of the function - "__run_wrapped();". It may appear more than
once.
The "wrap" may be activated on statements (include/require) similarly to the
__autoload() function, but a little better.
Which means, that every time that php comes up against a statement, it will
run the wrap function.
It may be implemented as the following:
/* PHP code */
// declare a wrap function for include
__wrap_include($params [, ...[, ...]] )
{
// do something
$returned_value = __run_wrapped(); // Run the wrapped include statement.
// do something else
}
$statement = 'include';
set_wrap($statement, $callback, $params);
// PHP encounters the include statement.
// So, it will run the __wrap_include() function
include 'some_file.php';
// or, another example:
__wrap_require_once($params [, ...[, ...]] )
{
// do something
$returned_value = __run_wrapped(); // Run the wrapped include statement.
// do something else
}
$statement = 'require_once';
set_wrap($statement, $callback, $params);
// PHP encounters the require_once statement.
// So, it will run the __wrap_require_once() function
require_once 'some_file.php';
/* End of PHP code */
The ser_wrap mechanism may enable the php programmers to create their own
statements. It's no a must of course, but I'm convinced that it will upgrade
the programmer's programming techniques.
What do you think?
Is it really might improve the php language?
Thanks in advance,
Dor (dor9990@gmail.com).