PHP 7, Apache 2, Debian 9 amd64
A few days ago I discovered that a classless function was not always but occasionally not seeing variables defined in the same file.
So now I have a very similar but even weirder problem in a static function inside a class container. Before you ask, I am writing this code because DOMDocument is a broken joke which should not even be in PHP Distributions in its current state.
A few days ago I discovered that a classless function was not always but occasionally not seeing variables defined in the same file.
Code:
<?php
$someArr = array( 'def0' => 'val0', 'def1' => 'val1', 'def2' => 'val2' );
function SomeFunc( $someParam )
{
global $someArr;
/*
Search into $someParam for keys found in $someArr and perform replacements.
Sometimes $someArr is empty, sometimes $someArr is unset.
The problem depends on which PHP file is calling it, and yes I am anal about require_once.
The fix for this was to change the function signature and then pass $someArr into the function
every time, as a parameter, which worked fine from every consumer.
*/
}
?>
So now I have a very similar but even weirder problem in a static function inside a class container. Before you ask, I am writing this code because DOMDocument is a broken joke which should not even be in PHP Distributions in its current state.
Code:
<?php
class HTML_Element
{
public static function CreateElementsFromHtml( $htmlString )
{
echo 'htmlString is ' . $htmlString . '<br/>'; // -> $htmlString is always empty
$r = array();
$htmlChars = str_split( $htmlString );
print_r( $htmlChars ); // -> This works fine, spits out all the characters which were input into $htmlString
echo '<br />';
$tagStack = array();
echo 'htmlString is ' . $htmlString . '<br/>'; // -> $htmlString is always empty
/*
Attempting to increment or manually set the index in a for-loop
just hangs in PHP.
So I'll use my own index and do/while
As you read you may note that the same thing is happening in do/while...
*/
$i = 0;
do
{
$c = $htmlChars[ $i ];
if ( $c === '<' )
{
echo 'opener found<br/>'; // -> Because the array is always right, this works fine
$end = array_search( '>', $htmlChars, true ); // -> Because the array is always right, this works fine
if ( isset( $end ) && ( $end !== false ) && ( $end > -1 ) )
{
/*
Include the found character
*/
$length = ( ( $end + 1) - $i );
echo 'ender found<br/>';
echo 'index is ' . $i . '<br/>';
echo 'end is ' . $end . '<br/>';
echo 'length is ' . $length . '<br/>';
echo 'htmlString is ' . $htmlString . '<br/>'; // -> $htmlString is always empty
$tag = substr( $htmlString, $i, $length ); // -> $htmlString is always empty, so $tag is always empty
echo 'tag is ' . $tag . '<br/>'; // -> See line above
/*
Finally, increment $i past the end of the tag
*/
//$i += $length; // -> This causes PHP to hang indefinitely, both in for ( $i = 0; $i < count( $htmlChars ); $i++ ) and in do/while as shown
/*
When this hang occurs, PHP occasionally (but not always) spits out an
error log entry indicating that processing time took too long.
*/
//echo 'i is now ' . $i . '<br/><br/>'; // ->This line is ever ever reached
//continue;
}
}
/*
If we didn't reset $i earlier and continue, then
increment $i now
*/
$i++; // -> Works fine when reached
echo 'i is now ' . $i . '<br/><br/>'; // -> Works fine when reached
} while ( $i < count( $htmlChars ) );
return $r;
}
}
HTML_Element::CreateElementsFromHtml( '<html><head></head><body></body></html>' );
?>