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

Create instance creator operation #7

Merged
merged 1 commit into from
Sep 6, 2017

Conversation

jejung
Copy link
Contributor

@jejung jejung commented Sep 5, 2017

No description provided.

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
Copy link

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.

Copy link
Contributor Author

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
Copy link

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());
};
}
Copy link
Owner

@ihor ihor Sep 6, 2017

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:

  1. Starting PHP 5.6 we have the splat operator .... Using we can get rid of reflection which is relatively slow and have the following code return new $class(...func_get_args());. In my own tests, it makes the speed about two times faster.
  2. 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);
Copy link
Owner

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.

@ihor
Copy link
Owner

ihor commented Sep 6, 2017

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 master to 1.2. This way I'll be able to release this change in 1.2.1. Current master version contains lazy evaluations which will be released in 1.3.

Also, I would appreciate if you squashed the commits.

Thanks,
Ihor

@jejung jejung changed the base branch from master to 1.2 September 6, 2017 11:33
@jejung jejung changed the base branch from 1.2 to master September 6, 2017 11:34
@jejung jejung changed the base branch from master to 1.2 September 6, 2017 11:36
@jejung jejung changed the base branch from 1.2 to master September 6, 2017 11:36
@jejung jejung changed the base branch from master to 1.2 September 6, 2017 12:00
@jejung
Copy link
Contributor Author

jejung commented Sep 6, 2017

Thanks for your comments @daltones and @ihor, fixed all of them! Awesome to hear that you can apply this to 1.2.1

@ihor ihor merged commit f63fc22 into ihor:1.2 Sep 6, 2017
@ihor
Copy link
Owner

ihor commented Sep 6, 2017

Merged and released in 1.2.1. Thanks for the pull request!

@jejung jejung deleted the feature/instance-creator-op branch September 6, 2017 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants