Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

New test #360

Closed
wants to merge 2 commits into from

3 participants

Marcel Araujo Lior Kaplan Account for PHP Pull Requests
Marcel Araujo
  • PHP-5.5:

Each closure instance should have its own set of static variables. This works perfectly with non-generator closures (replace yield by return array in the test script), but not with generator closures (run the test script), where static variables seem to behave like a normal local variables.

Test created for the bug #64979

Lior Kaplan

Thanks for the pull-request.

Could edit the commit message to be more descriptive of the change than only linking to the bug ? See how other relate to fixing bug in the commit messages: https://github.com/php/php-src/commits/master

Account for PHP Pull Requests
Collaborator

Comment on behalf of stas at php.net:

merged

Account for PHP Pull Requests php-pulls closed this August 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
30  Zend/tests/bug64979.phpt
... ...
@@ -0,0 +1,30 @@
  1
+--TEST--
  2
+Bug #64578 (Closures with static variables can be generators)
  3
+--FILE--
  4
+<?php
  5
+
  6
+function new_closure_gen() {
  7
+	return function() { 
  8
+		static $foo = 0; 
  9
+		yield ++$foo; 
  10
+	};
  11
+}
  12
+
  13
+$closure1 = new_closure_gen();
  14
+$closure2 = new_closure_gen();
  15
+
  16
+$gen1 = $closure1();
  17
+$gen2 = $closure1();
  18
+$gen3 = $closure2();
  19
+
  20
+foreach (array($gen1, $gen2, $gen3) as $gen) {
  21
+  foreach ($gen as $val) {
  22
+    print "$val\n";
  23
+  }
  24
+}
  25
+
  26
+?>
  27
+--EXPECT--
  28
+int(1)
  29
+int(2)
  30
+int(1)
26  Zend/tests/closure_047.phpt
... ...
@@ -0,0 +1,26 @@
  1
+--TEST--
  2
+Closure 047: Use in preg_replace_callback() using variables by reference
  3
+--FILE--
  4
+<?php
  5
+
  6
+function replace_variables($text, $params) {
  7
+	
  8
+	preg_replace_callback( '/(\?)/', function($matches) use (&$params, &$text) {
  9
+	
  10
+		$text = preg_replace( '/(\?)/', array_shift( $params ), $text, 1 );
  11
+	
  12
+	}, $text );
  13
+	
  14
+	return $text;
  15
+}
  16
+
  17
+echo replace_variables('a=?', array('0')) . "\n";
  18
+echo replace_variables('a=?, b=?', array('0', '1')) . "\n";
  19
+echo replace_variables('a=?, b=?, c=?', array('0', '1', '2')) . "\n";
  20
+echo "Done\n";
  21
+?>
  22
+--EXPECT--
  23
+a=0
  24
+a=0, b=1
  25
+a=0, b=1, c=2
  26
+Done
26  Zend/tests/closure_048.phpt
... ...
@@ -0,0 +1,26 @@
  1
+--TEST--
  2
+Closure 048: Use in preg_replace_callback() using variables by reference
  3
+--FILE--
  4
+<?php
  5
+
  6
+function replace_variables($text, $params) {
  7
+	
  8
+	$c = function($matches) use (&$params, &$text) {
  9
+		$text = preg_replace( '/(\?)/', array_shift( $params ), $text, 1 );
  10
+	};
  11
+
  12
+	preg_replace_callback( '/(\?)/', $c, $text );
  13
+	
  14
+	return $text;
  15
+}
  16
+
  17
+echo replace_variables('a=?', array('0')) . "\n";
  18
+echo replace_variables('a=?, b=?', array('0', '1')) . "\n";
  19
+echo replace_variables('a=?, b=?, c=?', array('0', '1', '2')) . "\n";
  20
+echo "Done\n";
  21
+?>
  22
+--EXPECT--
  23
+a=0
  24
+a=0, b=1
  25
+a=0, b=1, c=2
  26
+Done
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.