Autoload solution to __PHP_Incomplete_Class - Re: [PEAR] Can you serialize a Formbuilder object?

From: 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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">//a handy string function that returns everything in the string after your query <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//example after('abc','abcdef'); returns 'def' <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 /> &nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;}; <br /> <br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">//function __autoload($classname) <br />&nbsp;&nbsp;&nbsp;&nbsp;//this function gets called when a class is come across that has not been included yet <br />&nbsp;&nbsp;&nbsp;&nbsp;//there are two locations for classes that this program uses. <br />&nbsp;&nbsp;&nbsp;&nbsp;//the first is the DataObjects directory containing the autogenerated DataObject classes <br />&nbsp;&nbsp;&nbsp;&nbsp;//that were created with createTables.php <br />&nbsp;&nbsp;&nbsp;&nbsp;//and prepended with the class_prefix <br />&nbsp;&nbsp;&nbsp;&nbsp;//listed in DataObject.ini <br />&nbsp;&nbsp;&nbsp;&nbsp;//the second is the PEAR library repository.&nbsp;&nbsp;Per PEAR conventions, the class name will <br />&nbsp;&nbsp;&nbsp;&nbsp;//determine where the file is located in the PEAR hierarchy. <br />&nbsp;&nbsp;&nbsp;&nbsp;//example HTML_QuickForm_Header class is located at the path HTML/QuickForm/header.php <br />&nbsp;&nbsp;&nbsp;&nbsp;// NOTE: required classes above are the ones that don't follow this convention. <br />&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">__autoload</font><font color="#007700">(</font><font color="#0000BB">$className</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">#echo $className."\n".$dataObjectClassPrefix."\n".strpos($className, $dataObjectClassPrefix)."&lt;br&gt;\n\n\n"; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br /></font><font color="#0000BB">?&gt;</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) >> >> >

« previous php.pear.general (#19825) next »