class_exists and inheritance

So I have a class foo that extends bar. Then I want to check if the class exists.



if( !class_exists( 'Foo' ):
class Foo extends Bar{

}endif;

if( !class_exists( 'Bar' ):
class Bar{
}
endif;

Gives me a fatal error.

Am I missing something obvious and basic?

Bar doesn’t exist when you create Foo…why are you checking if the class exists before creating it? That makes no sense.

I’m checking to make sure the class hasn’t been called before to prevent a fatal error. It works fine except when extending classes. The order usually goes:



require_once 'bar_class.php';

if( !class_exists( 'Foo' ) ):
class Foo extends Bar{
}
endif;


So Bar should exist before I call Foo. Yet I get the same error.

Take a look at the native autoload functionality.


<?php
function __autoload($class){
	require_once sprintf('%s_class.php', $class);
}

new Foo;
/*
	__autoload automattically 'requires'
	Foo_class.php, and then Bar_class.php
	if they are not already defined
*/

Remove the if statements. And only ever use include_once or require_once with pulling classes into code.