The script tried to execute a method or access a property of an incomplete object.

March 17, 2011 — Mike Hommé
Mike Hommé's picture

You're getting the error "The script tried to execute a method or access a property of an incomplete object" after trying to access a property or method of an object you've stored in a $_SESSION variable in your PHP.


You've probably got some code that looks like this:

session_start();
include("classes/Database.class.php");
include("classes/User.class.php");

... trying to do something with the object, it'll fail ...

Change it to this:

include("classes/Database.class.php");
include("classes/User.class.php");
session_start();

... trying to do something with the object, it'll work ...

As you can see including your class definitions before you start the session (where your object is) is what makes this work.

So fine, PHP needs to know about your class definitions before you can access a session object that makes use of that class, makes sense to me, now.

It's tripped me up a few times because a) I don't do a ton of PHP programming, so I forget, and b) I've always understood that session_start() should be the first line of code in a script. However, the latter is not the case if you're doing OOPHP and working with classes. I'll need to run this by my mentors, who are very procedural in their PHP.

Am I missing something, anyone?

Comments (2)

Guest's picture

Another Tip

Thanks for the info. I've been struggling with this error myself. For some reason it worked for me locally, but not in my test environment. Anyway, I found that the error could be resolved if you manually serialize the object before inserting it into the $_SESSION array, and deserializing it on the way out.

Your solution is cleaner, but I was forced to do things this way as I'm working with a CMS and it wasn't possible for me to load the class prior to the session_start() call.

Hope that's useful to someone else!

Chris

Mike Hommé's picture

Interesting

Yes, when tied (painted into a corner) with a particular CMS or other type framework I can see where you may have ran into trouble.

Glad you've got it working.