0

Question: How can I avoid getting an PHP Incomplete Class i this case?

Background: The code is a script receiving POST-variables from a form, performing some operations on \ $_SESSION['objectCart'], and finally re-directing the visitor to another page. $_SESSION['objectCart'] contains an instance of the 'cart' class defined in the file

__DIR__ . $lib . 'user.class.php'    

When I run the script it stops, and further investigation reveals the __PHP__Incomplete Class.

My code:

require_once __DIR__ . $lib . 'utility.class.php';
require_once __DIR__ . $lib . 'productcatalog.class.php';
require_once __DIR__ . $lib . 'user.class.php';
require_once __DIR__ . $lib . 'cart.class.php';

session_start();

print_r( $_SESSION['objectCart'] );

HTML output:

__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => cart [itemsExtended:protected] => Array ( [0] => Array ( [item_name] => ...

SOLUTION - PARTIAL:

A session was started already, as it normally is with this problem. It was identified by placing

echo session_id(); 

before the cart-class was initiated.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
MagnusN
  • 33
  • 7
  • Hi @MagnusN refer to here :- http://stackoverflow.com/questions/2010427/php-php-incomplete-class-object-with-my-session-data – Anjana Silva Jan 09 '15 at 09:04
  • I'm not sure you really need that thing you are trying to do, but instead of saving the raw instance into a session, save it serialized – Royal Bg Jan 09 '15 at 09:04
  • __php_incomplete classes are created when an unserialise failed. – GordonM Jan 09 '15 at 09:21
  • I attempted to use the serialize/unserialize commands, but with no luck. The output did not change. The weird thing is the script has worked for more than 6 months, never causing any problems like this, before yesterday. – MagnusN Jan 09 '15 at 09:38
  • Maybe the session gets (re)started before your call to session_start()? Could be another script (file) that is executed before your code, maybe triggered by an autoloader, maybe session.auto_start has been set or ...something else. Just for testing purposes you could add a call to `session_id()` before your session_start. If it returns something different than an empty string the session is already running. – VolkerK Jan 09 '15 at 10:31
  • Thank you, @VolkerK. I wasn't familiar with the session_id() function. After running it I feel sort of stupid, as it seems a session has been started somewhere already. Anyway thank you. – MagnusN Jan 12 '15 at 14:05

1 Answers1

0

There is a pretty good duplicated mentioned in the comments and as I commented, you might want to use serialize() here, however I find rarely a good reason to save objects in session for further use.

I'd rather have stored in the session the things I do need in order to create the objects and then from a single entry point to register the necessary objects. Of, use a database to save the needed fields and only store an identifier in the session.

Let's say you need information for the current logged user through all of you application, after successful login. Instead of assigning the instance of the User class into a session, I'd store its details into session, and construct the object each time a new page is requested.

It may add an additional overhead if you store the identifier and query the DB to take all the other info for the user upon each refresh, but still I would go for it, if I really don't see a significant application slowing.

main_entry_point.php:

if (isset($_SESSION['user_id'])) {
    $row = $db->query("SELECT name, email, age, role_id FROM users WHERE id = " . $_SESSION['user_id']);
    $user = new User($row['name'], $row['email'], $row['age'], $row['role_id']);
}

There can be a lot of further sugar here, like injecting the user instance to your application class if you have one, etc, etc, so you will not work with global variables ($user in this case is a pseudo-global variable for me, since you have a main entry point which is included on each refresh, like index.php in MVC frameworks), but that's not relevant here, the point was in long story short - to register your cart object from the particular things stored in the session.

Royal Bg
  • 6,988
  • 1
  • 18
  • 24