Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception on instantiating descendant of \ArrayObject #129

Closed
msjyoo opened this issue Nov 10, 2015 · 4 comments
Closed

Exception on instantiating descendant of \ArrayObject #129

msjyoo opened this issue Nov 10, 2015 · 4 comments
Labels

Comments

@msjyoo
Copy link

msjyoo commented Nov 10, 2015

Hello,

I think I've found a bug when auryn attempts to instantiate an object which is a descendant of \ArrayObject.

Exception / Stack trace:

PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php:400
Stack trace:
#0 [internal function]: ArrayObject->__construct(NULL)
#1 /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php(400): ReflectionClass->newInstanceArgs(Array)
#2 /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php(367): Auryn\Injector->provisionInstance('sekjun9878\\Func...', 'sekjun9878\\func...', Array)
#3 /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php(522): Auryn\Injector->make('sekjun9878\\Func...')
#4 /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php(459): Auryn\Injector->buildArgFromTypeHint(Object(ReflectionMethod), Object(ReflectionParameter))
#5 /h in /project/g/php-typechecker/vendor/rdlowrey/auryn/lib/Injector.php on line 400

Class signatures:

final class FunctionDirectory extends Directory
abstract class Directory extends \ArrayObject

Workaround:

//$functionDirectory = $injector->make(FunctionDirectory::class);
$functionDirectory = new FunctionDirectory;
$injector->share($functionDirectory);
@msjyoo msjyoo changed the title Exception on instantiating desendant of \ArrayObject Exception on instantiating descendant of \ArrayObject Nov 10, 2015
@kelunik
Copy link
Collaborator

kelunik commented Jan 9, 2017

Could you share a full reproducible code snippet, please?

@msjyoo
Copy link
Author

msjyoo commented Jan 9, 2017

<?php

require_once(__DIR__."/vendor/autoload.php");

final class FunctionDirectory extends Directory_ {}
abstract class Directory_ extends \ArrayObject {}

$injector = new Auryn\Injector;

$functionDirectory = $injector->make(FunctionDirectory::class);
PHP Fatal error:  Uncaught TypeError: ArrayObject::__construct() expects parameter 3 to be a class name derived from Iterator, '' given in /home/sekjun9878/Documents/Development/Projects/test/vendor/rdlowrey/auryn/lib/Injector.php:406
Stack trace:
#0 [internal function]: ArrayObject->__construct(NULL, NULL, '')
#1 /home/sekjun9878/Documents/Development/Projects/test/vendor/rdlowrey/auryn/lib/Injector.php(406): ReflectionClass->newInstanceArgs(Array)
#2 /home/sekjun9878/Documents/Development/Projects/test/vendor/rdlowrey/auryn/lib/Injector.php(373): Auryn\Injector->provisionInstance('FunctionDirecto...', 'functiondirecto...', Array)
#3 /home/sekjun9878/Documents/Development/Projects/test/test.php(10): Auryn\Injector->make('FunctionDirecto...')
#4 {main}
  thrown in /home/sekjun9878/Documents/Development/Projects/test/vendor/rdlowrey/auryn/lib/Injector.php on line 406

@kelunik
Copy link
Collaborator

kelunik commented Jan 9, 2017

This happens due to PHP not providing reflection information, see https://3v4l.org/plOeU.

While we could fix this particular case by omitting all remaining optional parameters, I think this should be solved in PHP itself providing better reflection information. In the end, it will break once one specifies a last parameter somewhere with the parameters before also optional and without provided reflection information.

In this case null is fine for the first two parameters, but not for the last one. In other cases this might be different.

You can solve this by explicitly providing the exact same constructor in userland, e.g. in Directory_. Another solution is to just define those parameters using Injector::define.

koenhoeymans pushed a commit to koenhoeymans/auryn that referenced this issue Aug 7, 2018
Danack pushed a commit to Danack/Auryn that referenced this issue Dec 9, 2022
@Danack
Copy link
Collaborator

Danack commented Dec 21, 2022

A mere seven years later, fixed in 113f8c7

@Danack Danack closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants