views:

1657

answers:

4

I'm getting an error I don't know how to fix so I wondering if I could get some help.

This is the error

Fatal error: process_form() [<a href='function.process-form'>function.process-form</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;Template&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/twinmeddev/html/template_add.php on line 44

I get this error in the process_form() function. So what I get out of this is that, its thinking I didn't load the class for the template. Which in fact I did up at the top. The include 'inc/item.class.php'; Do I have to re-include it in the function?

Here's the code for the particular page with the error. You can see I have everything included like it should be. Where have I gone wrong?

<?php
include 'inc/prep.php';
include 'inc/header.class.php';
include 'inc/item.class.php';
include 'inc/template.class.php';
include 'inc/formhelper.class.php';
include 'inc/formvalidator.class.php';
include_once( 'inc/config/config.php' ) ;
include_once( 'inc/DBE.class.php' ) ;
include_once( 'inc/GenFuncs.php' ) ;
include_once( 'inc/Search.class.php' ) ;

session_start(); 

//Verify that user is logged in.
VerifyLogin($_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']);

if(array_key_exists('_submit',$_POST)) {
 if($fv_errors = validate_form()) {
  show_form($fv_errors);
 } else {
  process_form();
 }
}
else {
 // The form wasn't submitted or preview was selected, so display
 show_form();
}

function validate_form(){
}

function process_form(){
 global $mysqli;

 echo var_dump($_SESSION);

 $Template = $_SESSION['template'];
 $Template->name = $_POST['name'];
 $Template->descript = $_POST['descript'];
 $Template->user = $_SESSION['User'];
 $Template->customer = $_SESSION['CustID'];
 $Template->status = $_POST['status'];
 $Template->insert();

 //header("Location: template.php");
}
+1  A: 

It's missing the serialize/unserialize of your template class.

Take a look here for an working example I gave on another question of yours.

For instance, you probably want this:

<?php
  $_SESSION['template'] = serialize($template);
?>

and

<?php
  $template = unserialize($_SESSION['template']);
?>

Edit:

reading your comment about moving it to the top gives one hint.

The automatic serialization/unserialization occurs when you call session_start().
That means the order in which you include your files and call the session_start() is very important.

For example:

This would be wrong:

<?php
session_start();
include 'inc/template.class.php';
?>

While this would be correct:

<?php
include 'inc/template.class.php';
session_start();
?>

Now, I see in your example that it is in the CORRECT order, but I also notice you do many other includes before including template.class.php

Would it be possible that one of those includes (perhaps prep.php or header.class.php) does call start_session() too?
If yes, that was your issue (session_start() being called before your template.class.php).

Carlos Lima
From what I understand PHP automatically serialize an object if you add it to a session and then unserialize it if you were to assign it to a variable. At least that's what I've read here: http://www.devshed.com/c/a/PHP/Working-with-MySQL-and-Sessions-to-Serialize-Objects-in-PHP/1/Everything involving my object was working nicely except this.
MackDaddy
Taking the example i gave you on the other post and removing the serialize/unserialize will get you this: Unserialized $template: object(__PHP_Incomplete_Class)#1 (3) { ...}
Carlos Lima
Don't take my word for it, try the code I gave you as example, then try adding un/serialize to your code and see if it works :)
Carlos Lima
@MackDaddy : in the sense of contributing back to the community you should consider going back to your questions to give the appropriate upvotes as well as tagging the 'accepted answer'. If no answer were what you were looking for, you can add an answer of your own with whatever solved your problem.
Carlos Lima
Well with the three PHP questions I've asked here, none of the answers worked. Serializing produced the same results unforntunately. Interestingly enough, another problem was solved from you answer in my other question. What I just had to re-attach the database before I tried inserting it, because it wasn't recognizing my mysqli object, serialized or not after it was stored in a session. Don't quite understand why all the objects work but not mysql.This issue was fixed by simply moving the class include all the way to the top before that other includes. What would this be indicative of?
MackDaddy
In the other post, what I explained to you in details was *exactly* that the mysqli object wasn't being "unserialized" because you cannot serialize/unserialize resources (file handles, database handles, network sockets, etc).
Carlos Lima
Edited my answer according to your info that moving it to the top seems to solve your question. Can you check and reply saying if that was the case? :)
Carlos Lima
Hit it right on the nail, one of the classes was calling session_start(). Thanks for your help and working through this with me. You rock!
MackDaddy
A: 

How big are the objects that you are putting in the session? I got a similar error once when my objects were larger than the space we had allotted for the session in the php.ini file. If the object was too large, it would come back as being empty when it shouldn't have been.

After that, I just started storing the object's PK in the Session and looking it up if I needed it instead of carrying around the entire object itself.

You could also increase the size of the session in the INI file and see if that makes a difference.

AndyMcKenna
A: 
VolkerK
`require` and `include` are identical in every way except how they handle failure. They both produce a warning, but `require` results in a fatal error.
Philippe Gerber
A: 

i tried to store objects in session variable too. and yes i experienced the same problem as MackDaddy. and its solved by moving the include of the class to the first.

so..

require_once("class.someclass.php");
require_once("display.php");

worked

but not...

require_once("display.php");
require_once("class.someclass.php");

hmm...wonder whats the rational? and what if we have 2 or more different objects? which one comes first?

restless