Created
September 27, 2013 12:01
-
-
Save carbontwelve/6727555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* TestArrayWalk | |
* | |
* This is the code I have used when describing an issue I had with array_walk, closures and referencing $this | |
* within PHP version 5.3.3. | |
* | |
* The original stackoverflow question can be seen here: | |
* http://stackoverflow.com/questions/19033184/using-this-when-not-in-object-context-error-within-array-walk | |
* | |
* The TestArrayWalk class will not work in 5.3.3 while the TestForEach class will. They both do exactly the | |
* same thing, just using slightly different code. | |
*/ | |
Class TestArrayWalk | |
{ | |
/** @var null|array */ | |
protected $userInput = null; | |
/** | |
* This expects to be passed an array of the users input from | |
* the input fields. | |
* | |
* @param array $input | |
* @return void | |
*/ | |
public function setUserInput( array $input ) | |
{ | |
$this->userInput = $input; | |
// Lets explode the users input and format it in a way that this class | |
// will use for marking | |
array_walk( $this->userInput, function( &$rawValue ) | |
{ | |
$rawValue = array( | |
'raw' => $rawValue, | |
'words' => $this->splitIntoKeywordArray( $rawValue ), | |
'marked' => false, | |
'matched' => array() | |
); | |
} | |
); | |
} | |
public function getUserInput() | |
{ | |
return $this->userInput; | |
} | |
protected function splitIntoKeywordArray( $input ) | |
{ | |
if ( ! is_string( $input )){ return array(); } | |
return preg_split('/(\s|[\.,\/:;!?])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); | |
} | |
} | |
Class TestForEach | |
{ | |
/** @var null|array */ | |
protected $userInput = null; | |
/** | |
* This expects to be passed an array of the users input from | |
* the input fields. | |
* | |
* @param array $input | |
* @return void | |
*/ | |
public function setUserInput( array $input ) | |
{ | |
$this->userInput = $input; | |
// Lets explode the users input and format it in a way that this class | |
// will use for marking | |
foreach( $this->userInput as &$rawValue ) | |
{ | |
$rawValue = array( | |
'raw' => $rawValue, | |
'words' => $this->splitIntoKeywordArray( $rawValue ), | |
'marked' => false, | |
'matched' => array() | |
); | |
} | |
unset($rawValue); // Clean up pointer | |
} | |
public function getUserInput() | |
{ | |
return $this->userInput; | |
} | |
protected function splitIntoKeywordArray( $input ) | |
{ | |
if ( ! is_string( $input )){ return array(); } | |
return preg_split('/(\s|[\.,\/:;!?])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); | |
} | |
} | |
$testArrayWalk = new TestArrayWalk(); | |
$testArrayWalk->setUserInput( | |
array( | |
'This is a test input', | |
'This is another test input' | |
) | |
); | |
var_dump( $testArrayWalk->getUserInput() ); | |
$testForeach = new TestForEach(); | |
$testForeach->setUserInput( | |
array( | |
'This is a test input', | |
'This is another test input' | |
) | |
); | |
var_dump( $testForeach->getUserInput() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment