PHP Session with an Incomplete Object
The error message Fatal error: process_form() [...] The script tried to execute a method or access a property of an incomplete object is one of the most confusing runtime exceptions in PHP. It occurs when your code attempts to use an object that unserialize() has reconstructed, but the class definition for that object was not loaded into memory at the moment of reconstruction.
In the specific scenario provided, the developer includes inc/item.class.php and inc/template.class.php at the top of the file, before calling session_start(). However, the fatal error occurs inside process_form() when accessing $_SESSION['template']. The root cause is a misunderstanding of how PHP handles session data relative to class autoloading.
When PHP stores an object in the $_SESSION superglobal, it does not store the actual object instance. Instead, it serializes the object into a string and saves that string to the server-side storage mechanism (files or Redis). When you retrieve data from $_SESSION later, PHP performs unserialize() on that string to reconstruct the object.
Here is the critical detail: unserialize() requires the class definition to exist in memory before it attempts to reconstruct the object. If the class was defined, but then destroyed or reloaded in a way that cleared the autoloader's cache of that specific file during a long-running request (less common) or, more importantly, if the code structure implies a dynamic loading context where the include happens too late, the error surfaces.
However, in standard PHP 7+ execution, the most frequent cause for this specific error pattern is not missing includes at the top of the file, but rather how session_start() interacts with class definitions relative to unserialize. Wait, that is a common misconception. Let's look closer at the code flow.
The user includes files at the top:
include 'inc/template.class.php';
// ... other includes ...
session_start();
Then later, they access $_SESSION['template'].
If the class Template is defined in inc/template.class.php, and that file is included before session_start(), why would the error occur? The answer lies in how the session data was originally saved versus how it is being read. If the session data was serialized on a previous request, or if the $_SESSION variable contains an object that was stored previously, PHP must have access to the class definition at the exact moment unserialize is called inside session_start() or immediately after retrieving it.
But there is a specific scenario where this fails: Class Name Mismatch. If the class defined in inc/template.class.php is not named exactly Template, but perhaps template (lowercase) or TemplateModel, PHP's unserialize() function will fail to find the class by the key stored in the session. The error message explicitly mentions the class definition "Template".
If the file inc/template.class.php defines a class named Template, and you retrieve it as $_SESSION['template'], that works fine because PHP is case-insensitive for array keys, but the class name itself must match.
However, there is a more subtle issue often overlooked: Autoloading vs. Manual Includes. The error message suggests providing a __autoload() (deprecated) or ensuring the class is loaded. In modern PHP, if you rely on spl_autoload_register, and your session data was serialized when using a different autoloader configuration, you might face this.
But let's look at the actual code structure provided. The error happens in process_form(). The class Template is included via include 'inc/template.class.php' at the very top of the page. This should be sufficient.
Is it possible the session data was created on a different server or environment where the class definition did not exist? Yes. If you are using a shared hosting environment where the session file was written by a script that didn't have access to your custom includes, unserialize will fail because the class Template was never defined in memory when the data was saved (or rather, it cannot be reconstructed).
Wait, sessions are stored as strings. When you save an object to $_SESSION, PHP serializes it. If the script that saved the object did not have the class definition loaded, PHP will throw a fatal error immediately upon trying to serialize it, unless the object was created via a constructor that didn't require the full class (unlikely for complex objects).
Ah, here is the likely culprit: The session data might have been stored on a previous request where the class was not available, OR the class definition is being cleared.
Actually, looking at the error message again: Please ensure that the class definition "Template" ... was loaded _before_ unserialize() gets called.
This specific error usually happens when you retrieve an object from the session, but the class definition is not in memory. Since the user included the file at the top, why isn't it working?
One common pitfall: Global Scope and Class Visibility. If Template is defined as namespace App\Template, but you access it as new Template(), that fails. But here we are unserializing.
The most probable cause for this specific error in a legacy PHP application (suggested by the use of include and session_start() without modern framework abstractions) is that the session data was persisted to disk using a serialized string that references a class name, but on the current request, the class definition is not being recognized.
Could it be that include 'inc/template.class.php' is failing silently? If that file has a syntax error or returns false (if it were require), the next line would break. But if it's just an include, PHP continues.
Let's consider the $_SESSION['template'] retrieval. If this key contains an object of class Template, PHP calls unserialize($string). Inside that function, it looks up the class name in memory. It finds nothing? That means the include 'inc/template.class.php' didn't actually define a global class named Template.
Perhaps the file defines the class inside a namespace? Or perhaps the class name is template (lowercase) and the session key matches, but the error says "Template".
Actually, there is a known behavior in PHP where if you have session_start() before defining the class, it's fine. But if the session data was created in a separate process or a previous request that didn't define the class... no, you can't save an object to a session without defining the class first in that same process.
Unless! The session file is being read from disk, but the Template class is not included in memory.
Wait, the user says: "I did load the class for the template... Do I have to re-include it in the function?"
No, you do not need to re-include it. PHP includes are cached per request. However, if the error persists, check if inc/template.class.php is actually defining the class with the exact name Template. If the file defines:
class template { ... } // lowercase
And you expect Template, does that matter? No, class names are case-sensitive in PHP.
If the error says "The script tried to execute a method or access a property of an incomplete object", it means unserialize failed to map the string to a class.
Solution: Ensure the class definition is loaded before session_start() only if you are creating new sessions with objects immediately. But for retrieving existing sessions, the class must be defined in memory. The user has included it at the top.
Is it possible that inc/template.class.php includes another file that causes a conflict? Or perhaps the Template class is being autoloaded by a different mechanism that conflicts with the manual include?
Actually, the most common reason for this error in production environments without frameworks like Laravel is file path issues or namespace mismatches. If the class is inside a namespace, e.g., namespace MyProject; class Template {}, and you treat it as global, unserialize might look for MyProject\Template. But standard unserialize expects the fully qualified name to match the serialized string.
If the session data was saved when the code had no namespace (global scope), but now the file has a namespace, or vice versa, unserialize will fail because the class names don't match.
Recommendation:
- Verify that
inc/template.class.phpdefines the class exactly asclass Templatein the global namespace (no namespace declaration) if the session data was saved without one. - Ensure
include 'inc/template.class.php'is executed beforesession_start()and before any access to$_SESSION['template']. In the provided code, this is done. - Check for typos in the class name inside the file vs. the error message.
- If using namespaces, ensure the session data was serialized with the correct FQN (Fully Qualified Name).
If you are unsure about the history of the session data (e.g., migrated from a different server), the safest fix is to clear the session and re-initialize the user's state, or ensure the class definition is robustly loaded via spl_autoload_register if dynamic loading is required.
Regarding the specific code snippet, moving the include 'inc/template.class.php' inside process_form() is unnecessary and inefficient. The current structure at the top of the file is correct. The issue lies in the mismatch between the serialized class name in the session storage and the class definition currently loaded in memory. Ensure the class definition in inc/template.class.php matches exactly how it was originally defined when the data was stored.