PHP Classes

Iteration issue

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  All threads  >  Iteration issue  >  (Un) Subscribe thread alerts  
Subject:Iteration issue
Summary:posible bug in GetInputs method
Messages:6
Author:Matías montes
Date:2006-11-24 04:08:59
Update:2006-11-27 21:14:13
 

  1. Iteration issue   Reply   Report abuse  
Picture of Matías montes Matías montes - 2006-11-24 04:08:59
Manuel,

Debbugging my FCK editor plugin i've found that there is an non documented issue in the internal iteration of arrays such as the $inputs attribute.
I always iterate whole arrays using the foreach structure so I can guarantee that it will start on the very first element of the array and finish with the very last.
This kind of iteration works with the internal pointer of the array but it will ALWAYS reset before begining and will always leave the pointer at the end.
Throughout the forms class implementation I've seen repeated uses of the current, next, and key functions, which also work with the internal pointer and therefore are incompatible with my iteration of the $inputs attribute.
That's okay since $inputs should not be accesed directly.
The thing is that i needed to iterate through all the inputs set in the form on the ClassPageHead function. So in order to not use the foreach structure, I tried iterating the getinputs function... but it's implementation is also used using the internal pointer.

It's really easy to work around this issue... iterating an array is among the very first thing we learn how to program, but i think it should be better to reimplement at least one of this functions or, at the very least, document the issue.

Regards

Matías

PS: I'll post my workaround method tomorrow when I get to the office for anyone who needs it as a temporary solution

  2. Re: Iteration issue   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-11-24 21:15:55 - In reply to message 1 from Matías montes
As you well noted, you should not be accessing the inputs class variable directly. That is why it is not documented as public variable.

Another point, ClassPageHead is only for generating HTML that is included in the page head section and can only be included once in the page. It should not depend on which customs of the class are included in the form.

If you need to generate HTML for the head section that depends on which custom inputs are included in the class, you should do that in the PageHead function instead.

  3. Re: Iteration issue   Reply   Report abuse  
Picture of Matías montes Matías montes - 2006-11-27 01:17:58 - In reply to message 2 from Manuel Lemos
The reason I'm doing the call is simple.
FCKEditor checks if there is a javascript function called FCKeditor_OnComplete was defined when the editor fineshes loading.
This event behaviour is defined in the FCKEditor core and therefore I respected it so a fresh install of the editor would be compatible with the FCKEditor plugin.
Since the name of the function is the same for all the editor instances I should only define it once (on the page head) but as every instance could have different behaviour when the event triggers, each one should be included inside the same function evaluating which individual instance made the call.
At least, that's the best solution that I found.
I'm open to any suggestions on this matter.

Now, the point I was trying to make in the previous post, was that a call to a global function such as GetInputs should not disrupt the work of an active procedure (such as ClassPageHead).

Needless to say that my first implementation in which I accessed the inputs variable directly was completely wrong, and I have a new, fixed version of the plugin.

Matias

  4. Re: Iteration issue   Reply   Report abuse  
Picture of Matías montes Matías montes - 2006-11-27 04:49:02 - In reply to message 3 from Matías montes
As promised, this is the implementation used to avoid the "iteration issue".
Sorry I couldn't post it earlier.

$inputs = $form->GetInputs();
foreach ($inputs as $input){
some code...
}

In my case, I coud't use the GetInputs function because of the "iteration issue" and replaced that line with:

$inputs = array_keys($form->inputs);


  5. Re: Iteration issue   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-11-27 15:18:06 - In reply to message 4 from Matías montes
I have looked at the issue closer and can see what is the problem now.

I have just fixed the forms class functions PageHead, PageLoad and PageUnload functions to separate the iteration over the inputs array from the calls to the custom input classes.

So now you can safely call GetInputs function from your custom input classes.

I have not uploaded the class yet, but you may get it from the Web interface to its CVS repository:

meta-language.net/cvs/forms/forms.p ...

  6. Re: Iteration issue   Reply   Report abuse  
Picture of Matías montes Matías montes - 2006-11-27 21:14:13 - In reply to message 5 from Manuel Lemos
Many thanks for your attention and quick reply!

Matías