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

once exercise HELP!! #1

Open
jbigorra opened this issue May 11, 2018 · 0 comments
Open

once exercise HELP!! #1

jbigorra opened this issue May 11, 2018 · 0 comments

Comments

@jbigorra
Copy link

jbigorra commented May 11, 2018

Hi @danrevah , I was wondering if you could help me with this exercise. I haven't been able to figure out how to not share the same scope for the $n variable when the two calls $init1 = once($value); and $init2 = once($value) are done in testMultiUse().

This is what've got so far:

<?php

function once($func)
{
  $result;
  return function () use ($func) {
    static $n = 2;
    global $result;

    $arg = func_get_args()[0];
    $n--;
    if ($n > 0) {
      $args = func_get_args();
      $nArgs = func_num_args();
      if ($nArgs === 1) $result = call_user_func_array($func, $args);
      elseif ($nArgs > 1) $result = call_user_func($func, ...$args);
    }
    return $result;
  };
}

You think you can give me a hand to finish it completely?
Thanks!

Edit:

Almost there:

PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

...F                                                                4 / 4 (100%)

Time: 5.11 seconds, Memory: 4.00MB

There was 1 failure:

1) OnceTest::testMultiUse
Failed asserting that 1 matches expected 2.

/home/jbigorra/Projects/php-exercises/once/test.php:63

FAILURES!
Tests: 4, Assertions: 13, Failures: 1.
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

No branches or pull requests

1 participant