Autoload solution to __PHP_Incomplete_Class - Re: [PEAR] Can you serialize a Formbuilder object?
| From: | Andrew Wooldridge | Date: | Wed, 29 Jun 2005 16:58:34 +0000 |
| Subject: | Autoload solution to __PHP_Incomplete_Class - Re: [PEAR] Can you serialize a Formbuilder object? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19825@lists.php.net to get a copy of this message | ||
Thanks to all who were involved yesterday in the thread about formbuilder
and php sessions. The code I wrote yesterday as a solution is posted below.
Current issue: When I flip back and forth through my quickform controller
pages, a new $fb object is added to my $_SESSION array every time. I'd like
to overwrite the current $fb object with changed values if the user fills
out form incorrectly or goes back to a previous page to change the values.
I'll let you know about the progress. -A
For your review/comments:
<html><div style="border: solid 1px orange; padding: 20px; margin:
20px"><code><font color="#000000">
<font color="#0000BB"><?php
<br /></font><font color="#007700">require_once(</font><font
color="#DD0000">'/usr/lib/php/HTML/QuickForm/Controller.php'</font><font
color="#007700">);
<br />require_once(</font><font
color="#DD0000">'/usr/lib/php/DB/DataObject/FormBuilder.php'</font><font
color="#007700">);
<br />require_once(</font><font
color="#DD0000">'/usr/lib/php/HTML/QuickForm.php'</font><font
color="#007700">);
<br />require_once(</font><font
color="#DD0000">'DB/DataObject/FormBuilder/QuickForm.php'</font><font
color="#007700">);
<br />
<br /></font><font color="#FF8000">// Load some default action handlers
<br /></font><font color="#007700">require_once( </font><font
color="#DD0000">'HTML/QuickForm/Action/Next.php'</font><font
color="#007700">);
<br />require_once( </font><font
color="#DD0000">'HTML/QuickForm/Action/Back.php'</font><font
color="#007700">);
<br />require_once( </font><font
color="#DD0000">'HTML/QuickForm/Action/Jump.php'</font><font
color="#007700">);
<br />require_once( </font><font
color="#DD0000">'HTML/QuickForm/Action/Display.php'</font><font
color="#007700">);
<br />require_once( </font><font
color="#DD0000">'HTML/QuickForm/Action/Submit.php'</font><font
color="#007700">);
<br />
<br /> </font><font
color="#FF8000">//a
handy string function that returns everything in the string after your query
<br /> //example
after('abc','abcdef');
returns 'def'
<br /> </font><font
color="#007700">function </font><font color="#0000BB">after
</font><font
color="#007700">(</font><font
color="#0000BB">$thisone</font><font
color="#007700">, </font><font
color="#0000BB">$inthat</font><font
color="#007700">)
<br /> {
<br
/> if
(!</font><font color="#0000BB">is_bool</font><font
color="#007700">(</font><font
color="#0000BB">strpos</font><font
color="#007700">(</font><font
color="#0000BB">$inthat</font><font
color="#007700">, </font><font
color="#0000BB">$thisone</font><font
color="#007700">)))
<br
/> re
turn </font><font color="#0000BB">substr</font><font
color="#007700">(</font><font
color="#0000BB">$inthat</font><font
color="#007700">, </font><font
color="#0000BB">strpos</font><font
color="#007700">(</font><font
color="#0000BB">$inthat</font><font
color="#007700">,</font><font
color="#0000BB">$thisone</font><font
color="#007700">)+</font><font
color="#0000BB">strlen</font><font
color="#007700">(</font><font
color="#0000BB">$thisone</font><font
color="#007700">));
<br /> };
<br />
<br /> </font><font
color="#FF8000">//function
__autoload($classname)
<br /> //this function gets called when a class is
come across that has not been included yet
<br /> //there are two locations for classes that
this program uses.
<br /> //the first is the DataObjects directory
containing the autogenerated DataObject classes
<br /> //that were created with createTables.php
<br /> //and prepended with the class_prefix
<br /> //listed in DataObject.ini
<br /> //the second is the PEAR library
repository. Per PEAR conventions, the class name will
<br /> //determine where the file is located in the
PEAR hierarchy.
<br /> //example HTML_QuickForm_Header class is
located at the path HTML/QuickForm/header.php
<br /> // NOTE: required classes above are the ones
that don't follow this convention.
<br />
<br /> </font><font
color="#007700">function
</font><font color="#0000BB">__autoload</font><font
color="#007700">(</font><font
color="#0000BB">$className</font><font
color="#007700">)
<br /> {
<br />
<br />
</font><font
color="#0000BB">$config </font><font color="#007700">=
</font><font
color="#0000BB">parse_ini_file</font><font
color="#007700">(</font><font
color="#DD0000">'DataObject.ini'</font><font
color="#007700">,</font><font
color="#0000BB">TRUE</font><font color="#007700">);
<br />
</font><font
color="#0000BB">$dataObjectClassPrefix </font><font
color="#007700">=
{</font><font color="#0000BB">$config</font><font
color="#007700">[</font><font
color="#DD0000">"DB_DataObject"</font><font
color="#007700">][</font><font
color="#DD0000">"class_prefix"</font><font
color="#007700">]};
<br
/> </font><font
color="#0000BB">$dataObjectDirectoryPath </font><font
color="#007700">=
{</font><font color="#0000BB">$config</font><font
color="#007700">[</font><font
color="#DD0000">"DB_DataObject"</font><font
color="#007700">][</font><font
color="#DD0000">"class_location"</font><font
color="#007700">]};
<br />
<br
/> </font><font
color="#FF8000">#echo
$className."\n".$dataObjectClassPrefix."\n".strpos($className,
$dataObjectClassPrefix)."<br>\n\n\n";
<br />
<br
/> </font><font
color="#007700">if (!(</font><font
color="#0000BB">strpos</font><font
color="#007700">(</font><font
color="#0000BB">$className</font><font
color="#007700">, </font><font
color="#0000BB">$dataObjectClassPrefix</font><font
color="#007700">) ===
</font><font color="#0000BB">false</font><font
color="#007700">))
<br /> {
<br
/> re
quire_once </font><font
color="#0000BB">$dataObjectDirectoryPath</font><font
color="#007700">.</font><font
color="#0000BB">after</font><font
color="#007700">(</font><font
color="#0000BB">$dataObjectClassPrefix</font><font
color="#007700">,</font><font
color="#0000BB">$className</font><font
color="#007700">) . </font><font
color="#DD0000">'.php'</font><font
color="#007700">;
<br
/>
<br /> }
<br /> else
<br /> {
<br
/> re
quire_once </font><font color="#0000BB">str_replace</font><font
color="#007700">(</font><font
color="#DD0000">'_'</font><font
color="#007700">, </font><font
color="#DD0000">'/'</font><font
color="#007700">, </font><font
color="#0000BB">$className</font><font
color="#007700">).</font><font
color="#DD0000">'.php'</font><font
color="#007700">;
<br /> }
<br /> }
<br /></font><font color="#0000BB">?></font>
</font>
</code></div>
</html>
On 6/27/05 4:58 PM, "Joshua Eichorn" <josh@bluga.net> wrote:
> This is just the way php works. There is no solution for php4 except
> for making sure all the classes are included before unserialize.
>
> If your using php5 you can use autoload to include the files as needed.
>
> Or if your really cool you could write your own unserializer that
> required the classes as it hit them.
> -josh
> Andrew Wooldridge wrote:
>
>> On the first creation of the $fb FormBuilder object, this HTML code is
>> generated by echo print_r($fb); (Notice the array elements are
>> QuickForm_header and QUickForm_hidden rather than __PHP_Incomplete_Class)
>>
>>
>