gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

Conflict spl_autoload_register together with class_exists #333

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi guys!

In short, if I'm using RedBeanPHP file rb.php together with spl_autoload_register function, then I get PHP warning[1]....

Because in getModelForBean function used conditional check

if ( !class_exists( $modelName ) ) //etc..

and second argument TRUE by default (call __autoload by default) and spl_autoload_register executing much earlier, than boolean returned[2].

Probably, this is PHP's "not as expected" behavior, probably not.. I think will not problem if in class_exists function add second argument FALSE, eg

if ( !class_exists( $modelName, FALSE ) ) //etc..

[1] R::load('posts', $id); will try to load model_posts.php file.. [2] Apropos, class_exists returns TRUE allways if used spl_autoload_register and if this file (and class) exist... Hmm :)

Thanks for reading my bad English )

gabordemooij commented 10 years ago

RedBeanPHP gives your environment a chance to load the class if it can't find one. This is how RedBeanPHP is supposed to work. Make sure your auto loaders do not throw exceptions or generate warnings.

We get a lot of similar questions on the forum: https://groups.google.com/forum/#!searchin/redbeanorm/autoloader/redbeanorm/fkXJ7M2ffns/EBkdfm0MOt0J

Hope this helps.

ghost commented 10 years ago

Hi gabordemooij!

Sorry for my persistence, but I don't understand you or you don't understand me :)

I wrote simple code example on php.net website http://www.php.net/manual/en/function.class-exists.php#113852

The problem is precisely in class_exists function, more precisely in second boolean argument. In my code really not exist file model_posts.php and my script ended with error.

I try changed second argument to FALSE and work fine now..

Regards.

lord2800 commented 10 years ago

The problem is that your autoloader is blindly attempting to load a file without first testing if that file exists. Fix your autoloader.

ghost commented 10 years ago

Thanks for reply, lord2800!

Yes, this is one of many "fix" ways. But what will be the result if the file is available or will be created in the future? Yet one hidden conflict?

lord2800 commented 10 years ago

Your autoloader should ignore things it can't find a map for so that other registered autoloaders can potentially find a valid map for the thing. If, at some point, you add the file in the right place for your autoloader, it should pick up the file and include it.

This being said, perhaps you should switch to a PSR-0 autoloader? That is its' default behavior.

ghost commented 10 years ago

lord2800, thanks for help to me.

But in my situation and my decision was enough to fork and add second argument in class_exists function. Yet untested, but works fine to me.