Is there a namespace aware alternative to PHP's class_exists()? -


if try using class_exists() inside method of class in php have specify full name of class--the current namespace not respected. example if class is:

 <?     namespace foo;      class bar{        public function doesbooclassexist(){             return class_exists('boo');        }     } 

and boo class (which autoloads) , looks this

   namespace foo;      class boo{        // stuff in here     } 

if try:

$bar = new bar(); $success = $bar->doesbooclassexist(); var_dump($success); 

you'll false... there alternative way without having explicitly specify full class name ( i.e. class_exits('foo\boo') )?

prior 5.5, best way use qualified class name:

public function doesbooclassexist() {     return class_exists('foo\boo'); } 

it's not difficult, , makes absolutely clear you're referring to. remember, should going readability. namespace imports handy writing, make reading confusing (because need keep in mind current namespace , imports when reading code).

however, in 5.5, there's new construct coming:

public function doesbooclassexist() {     return class_exists(boo::class); } 

the class pseudo magic constant can put onto identifier , return qualified class name resolve to.......


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -