ZEND_ARG_INFO reflections in midgard php bindings
Posted on 2009-01-16 14:42:20 EET.
Yesterday I finished ZEND_ARG_INFO implementation support in PHP bindings for Midgard. I am happy as I closed another ticket, so this nice feature will be also available for Vinland. Yes, I must say this is nice feature. However, at first I was very unhappy how it's implemented in ZE2.
For simple class implmentation you may use ZEND_ARG macros, quite simply.
midgard_connection::open($name):
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_connection_open, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
But for dynamic classes, like Mgdschema ones, things are more complicated.
get_by_guid($guid) method:
{ NULL, 0, NULL, 0, 0, 0, 0, 0, 1 },
{ "guid", sizeof("guid")-1, NULL, 0, 0, 0, 0, 0, 0 },
Especially, if you take into account fact, that method's arguments are defined with the same structure as method itself. And of course (as usual), you can not read about this in ZE2 documentation. Just investigate and hack sources.
Anyway, what does it mean for Midgard? Better reflection and nice (almost perfect) way to introspect all Midgard core API via PHP classes.
Let's look at simple example (I used the same code, provided in PHP docs):
$method = new ReflectionMethod('midgard_user', 'password');
$p = $method->getParameters();
foreach ($method->getParameters() as $i => $param)
{
printf(
"-- Parameter #%d: %s {\n".
" Class: %s\n".
" Allows NULL: %s\n".
" Passed to by reference: %s\n".
" Is optional?: %s\n".
"}\n",
$i, // $param->getPosition() can be used from PHP 5.2.3
$param->getName(),
var_export($param->getClass(), 1),
var_export($param->allowsNull(), 1),
var_export($param->isPassedByReference(), 1),
$param->isOptional() ? 'yes' : 'no'
);
}
And the output:
-- Parameter #0: username {
Class: NULL
Allows NULL: false
Passed to by reference: false
Is optional?: no
}
-- Parameter #1: password {
Class: NULL
Allows NULL: false
Passed to by reference: false
Is optional?: no
}
-- Parameter #2: hashtype {
Class: NULL
Allows NULL: false
Passed to by reference: false
Is optional?: yes
}
A real cool thing about such reflection is possibility to create one unified documentation system for code written purely in PHP and for that one written in C extension. If Zend introspection will allow this, we should have much better documentation with less amount of work. And this will be first cool thing provided by Zend :)