note 66140 added to language.oop5.magic

From: Date: Mon, 15 May 2006 20:54:49 +0000
Subject: note 66140 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-111342@lists.php.net to get a copy of this message
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. ---- Server IP: 216.194.113.175 Probable Submitter: 68.165.149.2 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- http://master.php.net/note/edit/66140 Del: integrated -- http://master.php.net/note/delete/66140/integrated Del: useless -- http://master.php.net/note/delete/66140/useless Del: bad code -- http://master.php.net/note/delete/66140/bad+code Del: spam -- http://master.php.net/note/delete/66140/spam Del: non-english -- http://master.php.net/note/delete/66140/non-english Del: in docs -- http://master.php.net/note/delete/66140/in+docs Del: other reasons-- http://master.php.net/note/delete/66140 Reject -- http://master.php.net/note/reject/66140 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111342) next »