2009年7月7日星期二

Re: [fw-mvc] autoloader issue with reflection class

newInstanceArgs distribute the elements of an array to the parameters of
__construct().

$args = array('a','b');
class A {
public function __construct ($first, $second) {
echo $first;
echo $second;
}
}
$r = new ReflectionClass ('A');
$r->newInstanceArgs ($args); // prints 'ab'


This is similar to call_user_func_array. I think the method
newInstance() is what you are looking for.

Seth Atkins schrieb:
> I'm not sure if this is a possible bug, or just something I don't
> understand about reflection classes. I've been trying to figure out why
> my custom validator is losing "data" that I am trying to pass to it in
> the form of options. I'm trying to pass an array of options.
>
> I've stepped through all the code with a debugger and it seems to be
> failing at the point that my custom validator is loaded. Up to the point
> in the code where it creates the instance of the class (see below the
> snippet from Zend_Form_Element, the _loadValidator method),
> $validator['options'] carries an array of data. At the point that the
> __construct method of the custom validator gets it, the array is reduced
> to just the value of the first array entry. Everything else vanishes.
> I'm not REAL familiar with reflection classes or how they work, but it
> looks to me like it is reducing my array of options to just one option.
>
> What am I missing here??
>
> ...
> $r = new ReflectionClass($name);
> if ($r->hasMethod('__construct')) {
> $instance = $r->newInstanceArgs((array) $validator['options']);
> } else {
> $instance = $r->newInstance();
> }
> ...
>
> class My_Validator_DoesntWork extends Zend_Validate_Abstract
> {
> protected $_options = array();
>
> function __construct($options)
> {
> $this->_options = $options;
> }
> .....
>
> Seth Atkins
>

--
Sebastian Krebs
http://www.kingcrunch2000.de
PGP key http://kingcrunch2000.de/public/0x44291B2B-pub.asc

没有评论: