php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #58739 Fatal Error: Class xxx not found
Submitted: 2009-06-26 14:20 UTC Modified: 2011-09-18 13:14 UTC
Votes:23
Avg. Score:3.9 ± 1.0
Reproduced:16 of 18 (88.9%)
Same Version:6 (37.5%)
Same OS:3 (18.8%)
From: felix9x at yahoo dot com Assigned:
Status: No Feedback Package: APC (PECL)
PHP Version: 5.2.10 OS: RHEL 5
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-06-26 14:20 UTC] felix9x at yahoo dot com
Description:
------------
My codebase worked fine with APC 3.0.19 & php 5.2.8.

After upgrading to php 5.2.10 with either APC 3.0.19 or 3.1.2 I get this error:

Fatal Error: Class xxxx Not found in file /something/file.php


Using default settings for APC.

Reproduce code:
---------------
Basically the code lookes like this:

classfile.php
---------------
class X {
  function foo() {
     $v = new Y();
  }
}

class Y {


}

tester.php
include( __FILE__ . '../classfile.php' );
$x = new X();

-- 
Get error:
Class Y not found in 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-24 10:07 UTC] giese at sedo dot de
Same Problem in 5.2.11
Would be great to fix this asap, since we cannot upgrade php or use APC anymore.
 [2009-09-28 09:49 UTC] rasmus@php.net
Your test case is bogus. __FILE__.'../classfile.php' makes no sense and you are not accessing Y at all, so I don't see where that error would come from.  Please file a proper bug report with an actual working test case.
 [2009-11-05 13:39 UTC] victor at phpconference dot es
Hi Rasmus, certainly this testcase is wrong, but this bug has been already reported with other testcase:
http://bugs.php.net/bug.php?id=48787
http://bugs.php.net/bug.php?id=48686

I have the same problem with my code but the testcase is considerably more complicated to explain but, in short, seems like that APC can not remember the classes that have already been compiled or loaded.
 [2009-11-06 08:29 UTC] mkolme at gmail dot com
Same here. Using PHP 5.2.11 and APC 3.0.19 or 3.1.3p1.

The only solution for the time being is using XCache or another opcode caching system.
 [2009-11-06 08:54 UTC] rasmus@php.net
48686 states quite clearly that he can reproduce it without APC as does 48787, although he seems a bit confused about that.  We still do not have an actual test case that reproduces this that is APC-specific.
 [2009-11-17 10:11 UTC] prgarcial at gmail dot com
Same problem here. Using CentOS 5.4 (fully updated), php-5.2.11 with the patch provided in http://bugs.php.net/bug.php?id=49098 and APC 3.0.19 or 3.1.3p1

It works fine if APC is disabled but if it is enabled I got the following error:
"Fatal error: Class 'PEAR' not found in MDB2/Driver/mysqli.php on line 495"

To reproduce the error I've used HTTP_Session2 and the script provided in the bug mentioned above.
 [2009-11-19 18:17 UTC] felix9x at yahoo dot com
I will try to test & make reproducible test case this weekend.
 [2009-11-23 18:01 UTC] whosagingbetter at gmail dot com
My error is identical to http://bugs.php.net/bug.php?id=48787 and though I can't say how or why, it is unquestionably APC-specific because the problem will not occur with APC disabled, and always occurs when APC is enabled.

Example follows. "Class not found" error occurs on the lines noted even though the class has been defined and repeatedly used before the "write" and "close" handlers are invoked.

[index.php]
include('session.php');
Session::start();

[session.php]
include('db.php');

class Session {
  public static function handler_open($i_savepath, $i_sessname) {
    return DB::connected();
  }

  public static function handler_close() {
    return DB::close();#### ERROR: Class DB not found ####
  }

  public static function handler_read($i_sid) {
    return DB::read($i_sid);
  }

  public static function handler_write($i_sid, $i_data) {
    return DB::save($i_sid, $i_data);#### ERROR: Class DB not found ####
  }

  public static function handler_destroy($i_sid) {
    return TRUE;
  }

  public static function handler_gc($i_maxlifetime) {
    return TRUE;
  }
  
  public static function start() {
    session_set_save_handler(
      array('Session', 'handler_open'),
      array('Session', 'handler_close'),
      array('Session', 'handler_read'),
      array('Session', 'handler_write'),
      array('Session', 'handler_destroy'),
      array('Session', 'handler_gc')
    );
    
    session_start();
  }
}

[db.php]
class DB {
  public static function connected() {
    return TRUE;
  }
  
  public static function close() {
    return TRUE;
  }
  
  public static function read($i_sid) {
    return TRUE;
  }
  
  public static function save($i_sid, $i_data) {
    return TRUE;
  }
}

Happy Thanksgiving.
 [2009-11-23 18:09 UTC] whosagingbetter at gmail dot com
Very sorry to flood the page but this is a simpler example (all in one script) with different messages that may be more helpful. And again, this is only with APC enabled.

[php]
<?php

class Session {
  public static function handler_open($i_savepath, $i_sessname) {
    return DB::connected();
  }

  public static function handler_close() {
    return DB::close();
  }

  public static function handler_read($i_sid) {
    return DB::read($i_sid);
  }

  public static function handler_write($i_sid, $i_data) {
    return DB::save($i_sid, $i_data);
  }

  public static function handler_destroy($i_sid) {
    return TRUE;
  }

  public static function handler_gc($i_maxlifetime) {
    return TRUE;
  }

  public static function start() {
    session_set_save_handler(
      array('Session', 'handler_open'),
      array('Session', 'handler_close'),
      array('Session', 'handler_read'),
      array('Session', 'handler_write'),
      array('Session', 'handler_destroy'),
      array('Session', 'handler_gc')
    );

    session_start();
  }
}

class DB {
  public static function connected() {
    return TRUE;
  }

  public static function close() {
    return TRUE;
  }

  public static function read($i_sid) {
    return TRUE;
  }

  public static function save($i_sid, $i_data) {
    return TRUE;
  }
}

Session::start();
die('foo');
[/php]

[output]
foo

Warning: Invalid callback Session::handler_write, class 'Session' not found in Unknown on line 0

Warning: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct () in Unknown on line 0

Warning: Invalid callback Session::handler_close, class 'Session' not found in Unknown on line 0
 [2010-01-28 08:26 UTC] timosha at gmail dot com
i confirm that this error occurs on 
php 5.2.11 and apc 3.0.19
php 5.3.1  and apc 3.1.3p1

but php 5.2.10 and apc 3.0.19 works fine.

here is the test case:

<?php
	class SessionHandler
	{
		public static function open($session_path, $session_name){
			return true;
		}

		public static function close(){
			return true;
		}

		public static function read($session_id){
			return null;
		}

		public static function write($session_id, $session_data){
			return true;
		}

		public static function destroy($session_id){
			return true;
		}

		public static function gc($maxlifetime){
			return true;
		}
	}

	session_module_name('user');
	session_set_save_handler(
		'SessionHandler::open'   ,
		'SessionHandler::close'  ,
		'SessionHandler::read'   ,
		'SessionHandler::write'  ,
		'SessionHandler::destroy',
		'SessionHandler::gc'     );

	echo session_start();
?>

--first run result
1
--second and other run's
1
Warning: Invalid callback SessionHandler::write, class 'SessionHandler' not found in Unknown on line 0

Warning: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0

Warning: Invalid callback SessionHandler::close, class 'SessionHandler' not found in Unknown on line 0

(of you should run this via httpd and mod_php)

this code fails only if callback functions defined as class static members. folowwing code works fine:

<?php

	class SessionHandler
	{
		public function open($session_path, $session_name){
			return true;
		}

		public function close(){
			return true;
		}

		public function read($session_id){
			return null;
		}

		public function write($session_id, $session_data){
			return true;
		}

		public function destroy($session_id){
			return true;
		}

		public function gc($maxlifetime){
			return true;
		}
	}

	$s = new SessionHandler();
	session_module_name('user');
	session_set_save_handler(
		array($s,'open')   ,
		array($s,'close')  ,
		array($s,'read')   ,
		array($s,'write')  ,
		array($s,'destroy'),
		array($s,'gc'     ));

	echo session_start();
?>

if you need more info I can run strace or run httpd via gdb.
 [2010-04-06 22:40 UTC] onlinestuff626 at gmail dot com
I can confirm this as well. And I can definitely confirm it 
is an APC problem. Disabling APC fixes it. 

CentOS 5.4
php 5.2.11
APC 3.1.3p1

I didn't notice this until APC was deployed on several 
production sites. Looks like I'll be switching to XCache. 
This a pretty serious bug imo.

FWIW it seemed intermittent for me. If I restarted the 
server and cleared the cache it fixed it. I initially found 
it after it had been running for a while. Maybe it has to do 
with a memory issue or something.
 [2010-04-06 23:40 UTC] rasmus@php.net
It's not really an APC bug.  It has to do with the shutdown 
order.  If you call session_write_close() manually at the end 
of your script it will work fine.  We are looking for 
solutions to this at the PHP level.  It affects other 
extensions as well.
 [2010-04-15 05:42 UTC] rosenberger at taoweb dot at
Same problem on my system.
PHP Fatal error:  Class 'DB' not found in
with APC enable.

OpenSuSE 11.1 tested with
PHP 5.2.11, PHP 5.2.12
APC 3.1.4-dev,3.1.3,3.1.3p1,3.1.2,3.0.19

without APC all works fine.

On a second system i'm runing
PHP 5.2.6 with APC 3.1.2
ALL WORKS FINE!

So from PHP 5.2.6 to 5.2.11 (or 5.2.10) there are some changes.

BR/Torsten
 [2010-04-15 05:44 UTC] rasmus@php.net
Torsten, yes, so call session_write_close() and it will work 
fine everywhere.
 [2010-04-15 06:14 UTC] rosenberger at taoweb dot at
session_write_close works fine !
I tried it before but there was a fault on my side with a header location.

THANKS Rasmus for the fast answer.
BR/Torsten
 [2010-06-01 15:52 UTC] adam at connectedventures dot com
We are seeing this problem with PHP 5.3.2 and APC 3.1.3p1. Confirmed that session data is not writing. Problem is fixed when restarting APC or disabling. We have additionally added a session_write_close() at the end of our script with no luck.

[...]
components\Session::start();
echo View::dispatcher();
session_write_close();
// nothing after this...
 [2010-07-28 02:47 UTC] timosha at gmail dot com
Seems to be fixed in php 5.3.3
 [2010-08-02 01:26 UTC] timosha at gmail dot com
No. php 5.3.3 steel affects. But it doesn't throw warning any more.
 [2011-01-21 04:33 UTC] alex at alexforencich dot com
Confirmed with latest APC 3.1.6 and PHP 5.3.5 on FreeBSD 8.1.  The issue does not occur eAccelerator, so this is definitely a significant APC bug and it's definitely NOT bogus.
 [2011-01-21 04:43 UTC] pierre dot php at gmail dot com
@ alex at alexforencich dot com

Do you use the same script than reported earlier or do you 
have something specific? If the latter, please post it here.
 [2011-02-01 05:23 UTC] phpbugs at colin dot guthr dot ie
Another "Me too" comment just so I can subscribe to this bug for future updates (I wish there was an obvious way to do that).
 [2011-02-14 11:11 UTC] rowan dot collins at gmail dot com
Hi,

Sorry to clutter the bug, but I have an even simpler way to reproduce this bug - no custom session handlers are required, just any static reference to a class while serializing the session.

As with previous examples, calling session_write_close() rather than leaving PHP to write the session automatically causes it to work fine. It also works normally the first time APC compiles it after an edit or restart.

Source:

<?php
class Example
{
	private static $f = ' [Static Var Exists] ';
	
	public function __sleep()
	{
		echo ' [Class ' . __CLASS__ .
			( class_exists(__CLASS__) ? ' exists' : ' does not exist' )
		. '] ';
		echo self::$f;
		return array();
	}
}

session_start();

$_SESSION['x'] = new Example('hello');

echo 'Output something, then leave PHP to write session... ';
?>

Output when run from APC cache (PHP 5.2.17, APC 3.1.6):

Output something, then leave PHP to write session... [Class Example does not exist] 
Fatal error: Access to undeclared static property: Example::$f in /home/rowanc/development/cwt/sites/JTATravelGroup/JTATrade/trunk/htdocs/static.php on line 12
 [2011-02-14 11:44 UTC] rasmus@php.net
Once again, the fix is trivial. Put session_write_close() in 
your script when you are done with the session.

We'll come up with a fix eventually, but it isn't a trivial 
thing to fix.
 [2011-03-15 09:58 UTC] cvives at infoactiu dot com
An easy way to apply rasmus fix to your scripts if you don't know when or if they're exiting is registering a shutdown function:

register_shutdown_function("session_write_close");
 [2011-07-25 16:19 UTC] phpbugs at colin dot guthr dot ie
Hi guys,

Just wondering if there is any movement on this?

I seem to be getting bitten by this pretty hard. In production environment it doesn't happen too often (it may not happen at all) but in our dev env it happens so often I have to disable EPC. The primary change appears to be PHP version (production is still on 5.2.x).

Using the session_write_close() trick does not work for me :(

It seems that mmap mode is less prone to this than pthread mutexes. With the latter I get pretty much one request only before it breaks (the request where APC cache itself is built up).

I tried both using the register_shutdown_function() and calling s_r_c() manually, but both don't actually help.

The software in question is Joomla + a custom platform bolted on. I sadly can't really create a cut down reproduction system easily but happy to test anything you have in mind.
 [2011-07-25 18:54 UTC] rasmus@php.net
Sorry Colin, if session_write_close() doesn't fix it, then it 
is a completely different issue and you haven't exactly given 
us anything to go on here. Try to narrow it down to a smaller 
reproducible case, then file a separate bug.
 [2011-07-26 10:05 UTC] phpbugs at colin dot guthr dot ie
Thanks rasmus. I took your advice and spend several hours debugging this today and I am now fairly certain it is a separate issue as you suspected. It is however, *very* confusing. I have reported this via http://pecl.php.net/bugs/bug.php?id=22839 although the lack of a good reproduction case will no doubt hinder it's resolution. I'll try not to spam this bug any longer :)
 [2012-06-20 17:40 UTC] mrrehbein at gmail dot com
<?php
// All in a single file, no includes, no requires.

ini_set('display_errors', 1);
error_reporting(-1);

// Work-around as suggested by cvives at infoactiu dot com and others fixes it
//   register_shutdown_function('session_write_close');

class Inner
{

}

class SessionHandler
{
    public function __construct()
    {
        var_dump(class_exists('Inner'));
    }

    public function method()
    {
        var_dump(class_exists('Inner'));
        return true;
    }
}

$handler = new SessionHandler();

session_set_save_handler(
      array($handler, 'method'), // open
      array($handler, 'method'), // close
      array($handler, 'method'), // read
      array($handler, 'method'), // write
      array($handler, 'method'), // destroy
      array($handler, 'method')  // gc
);

session_start();

?>

Actual results:
  On the 2nd+ reload, prints a few "true" and then a few "false"
  class "Inner" stops existing during shutdown

Expected results:
  print all "true"
  a defined class should not vanish

OS: CentOS 6.2 - x86_64
PHP: 5.3.3
APC: 3.1.3p1
versions installed via YUM

Updated APC to 3.1.10-beta via PECL and got the same results
 [2012-06-20 19:01 UTC] mrrehbein at gmail dot com
It appears that this may be an internals issue rather then an APC issue.

http://news.php.net/php.internals/46999
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Sep 15 12:01:26 2019 UTC