-
Notifications
You must be signed in to change notification settings - Fork 16
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
Create instance creator operation #7
Conversation
README.md
Outdated
@@ -373,6 +374,13 @@ Returns a function that returns method result for a given object on predefined a | |||
$userIds = map(methodCaller('getId'), $users); | |||
``` | |||
|
|||
##### instanceCreator($class) | |||
Returns a function that returns a new instance of a predefined class for given parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about Returns a function that returns a new instance of a predefined class, passing its parameters to the constructor.
? I guess it's more precise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will fix it
nspl/op.php
Outdated
@@ -168,3 +168,18 @@ function methodCaller($method, array $args = array()) | |||
return call_user_func_array(array($object, $method), $args); | |||
}; | |||
} | |||
|
|||
/** | |||
* Returns a function that returns a new instance of a predefined class for given parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
nspl/op.php
Outdated
return function() use ($class, $reflection_class) { | ||
return call_user_func_array(array($reflection_class, 'newInstance'), func_get_args()); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NSPL is focused on speed (which allows us to use this tool in our daily to day projects). There are two optimizations which could be made:
- Starting PHP 5.6 we have the splat operator
...
. Using we can get rid of reflection which is relatively slow and have the following codereturn new $class(...func_get_args());
. In my own tests, it makes the speed about two times faster. - Again the splat operator is faster than a function call so we can get rid of
func_get_args()
too. According to my tests, it makes the code about 2.5 times faster than the original version.
So the final code can be something like that:
function instanceCreator($class)
{
args\expects(args\string, $class);
return function(...$args) use ($class) {
return new $class(...$args);
};
}
We can use PHP_VERSION
constant and version_compare
function to provide a PHP 5.3 solution too.
nspl/op.php
Outdated
function instanceCreator($class) | ||
{ | ||
args\expects(args\string, $class); | ||
$reflection_class = new \ReflectionClass($class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use camel-cased names $reflectionClass
.
I like the idea. Please have a look at my comments and make changes. One thing I want to ask is to change the branch from Also, I would appreciate if you squashed the commits. Thanks, |
ac67f0e
to
14c362f
Compare
14c362f
to
fc7479b
Compare
7be2613
to
26cf732
Compare
26cf732
to
63dab36
Compare
Merged and released in |
No description provided.