note 66140 deleted from language.oop5.magic by cmb

From: Date: Sun, 10 Nov 2019 12:39:01 +0000
Subject: note 66140 deleted from language.oop5.magic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212987@lists.php.net to get a copy of this message
Note Submitter: taylorbarstow at google's mail service ---- I've just come accross something interesting relating to storing PHP5 objects in a session. If you don't provide an __autoload(), then you MUST load the class definition before calling session_start(). I guess that when you call session_start(), any objects in the session are unserialized then and there and placed into $_SESSION. If you don't provide the class definition before calling session_start(), your object will get the class __PHP_Incomplete_Class, and you won't be able to use it for anything. Examples: <?php session_start(); require_once 'MyClass.php'; $obj = new MyClass; $_SESSION['obj'] = $obj; ?> Works fine. Then on a subsequent page load: <?php session_start(); require_once 'MyClass.php'; $_SESSION['obj']->callSomeMethod(); ?> Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "MyClass" 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. But if you do this instead, it works fine: <?php require_once 'MyClass.php'; session_start(); $_SESSION['obj']->callSomeMethod(); ?> Hopefully in some future release of PHP, __PHP_Incomplete_Class will be smart enough to check for a class definition at time of use (method call or property operation), and, if the class exists, magically "complete" itself and turn into the desired object.

« previous php.notes (#212987) next »