PHP Classes

Very, very bad error...

Recommend this page to a friend!

      URI Rewrite Handler  >  All threads  >  Very, very bad error...  >  (Un) Subscribe thread alerts  
Subject:Very, very bad error...
Summary:In some circumstances this class will not work properly
Messages:2
Author:Lukasz Bugaj
Date:2012-02-03 16:56:53
Update:2012-02-04 18:06:46
 

  1. Very, very bad error...   Reply   Report abuse  
Picture of Lukasz Bugaj Lukasz Bugaj - 2012-02-03 16:56:53
I think this class not work as well... In constructor RewriteEngine class we got:



for ( $i = 0 ; $i < sizeof($this->bits) ; $i++ )
{
if ( $this->bits[$i] === '' )
{
unset($this->bits[$i]);
}
}



It seems to be allright, but it's only seems. Let's analyze this.
The loop begins.
For example, let bits hold four elements. Let the second and the third elements will be ''. What happend?
First loop - OK, sizeof(bits)=4 we got sometnig, 'if' condition is false.
Second loop - OK, sizeof(bits)=4, we got empty string, 'if' condition is true, we executing unset() function.
Third loop - ERROR!! sizeof(bits) is 3!! Why? because we executed unset() function in previous loop! We unset second element, so third element is now second, and fourth is now third. Result: the oryginal third element was never checked!

OK, for example let's do this:
modify 'for' loop:



$this->bits = explode('/', $_SERVER['REQUEST_URI']);
var_dump($this->bits);

for ( $i = 0 ; $i < $aa=sizeof($this->bits) ; $i++ )
{
echo "i: ".$i." sizeof: ".$aa."<br>";
if ( $this->bits[$i] === '' )
{
unset($this->bits[$i]);
}
}
var_dump($this->bits);




Put both files in subdirectory on your server, ie. public_html/aa/
Open in browser slightly modified addres:
<jour_domain>//aa//sample.php
The result will be something like this:
nomadsoft.pl/testsite/err.png

As you see, the one empty element was missing during loop.

PS. Sorry for my bad english.

  2. Re: Very, very bad error...   Reply   Report abuse  
Picture of satheeskumar satheeskumar - 2012-02-04 18:06:46 - In reply to message 1 from Lukasz Bugaj
Nice Work.Can u tell me how to use class in projects