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

Asked
Active3 hr before
Viewed126 times

6 Answers

classawarenamespacealternative
90%

I am not sure, but I think that its correct for the class_exists to search not in the namespace.. If it checks directly in the namespace how you will be able to check for class outside any namespaces from inside? – Svetoslav Apr 5 '13 at 17:13 ,If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:, 1 Note that this can't be used with dynamic class names. It will throw Fatal error: Dynamic class names are not allowed in compile-time ::class fetch - but if they reside in the current namespace you can assemble the fully qualified class name by hand: $fqcn = __NAMESPACE__.'\\'.$class_name – Brockley John Sep 1 '19 at 9:03 ,you'll get a false... is there an alternative way to do this without having to explicitly specify the full class name ( i.e. class_exits('Foo\Boo') )?

Prior to 5.5, the best way to do this is to always use the fully qualified class name:

public
function doesBooClassExist() {
   return class_exists('Foo\Boo');
}
load more v
88%

If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:,you'll get a false... is there an alternative way to do this without having to explicitly specify the full class name ( i.e. class_exits('FooBoo') )?,I namespaced a class to make use of aliases to provide shorthands for some long class names: Now there is some third party code that expects #code# in the globa...,I am manipulating the same file to manage two external api classes. One api class is based on namespaces, the other one is not. What I would like to do is some...

If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:

 < ?
 namespace Foo;

 class Bar {
    public
    function doesBooClassExist() {
       return class_exists('Boo');
    }
 }
load more v
72%

If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:,you'll get a false... is there an alternative way to do this without having to explicitly specify the full class name ( i.e. class_exits('FooBoo') )?,I just had a similar problem but the issue is basically that 'overrideMimeType' is not supported in IE (any version). So basically you need to do something like in the following articles:,EDIT: and if it doesn't, try class_alias to create an alias in the global namespace for that class. However, that would be an ugly solution...

If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:

 < ?
 namespace Foo;

 class Bar {
    public
    function doesBooClassExist() {
       return class_exists('Boo');
    }
 }

And Boo is a class (which properly autoloads) and looks like this

   namespace Foo;

   class Boo {
      // stuff in here
   }

if I try:

$bar = new Bar();
$success = $bar - > doesBooClassExist();
var_dump($success);
load more v
65%

Is there a namespace aware alternative to PHP's class_exists()?, marsapp/timeperiodhelper format() PHP | 3 months ago , Should I always call vector clear() at the end of the function? None | 3 months ago

1.2.public
function doesBooClassExist() {
   3.
   return class_exists('Foo\Boo');
   4.
}
5.6.public
function doesBooClassExist() {
   7.
   return class_exists(Boo::class);
   8.
}
9.
75%

class_exists — Checks if the class has been defined,enum_exists() - Checks if the enum has been defined,interface_exists() - Checks if the interface has been defined, This function checks whether or not the given class has been defined.

load more v
40%

Decide if and how to apply PSR-0 to Drupal modules/extensions.,PSR-0 is a class naming standard for better interoperability between PHP-based projects and enables autoloading of classes. It was adopted for Drupal core already.,The default naming and lookup pattern for namespaced code applies to all Drupal extensions.,For most other Drupal modules/extensions, that won't be the case. So why force it on everyone?

#1
I think that's a wrong design decision in the current views implementation.
I would suggest sth like

class\ drupal\ my_module\ views_MyStyle
class\ drupal\ my_module\ views\ MyStyle
class\ drupal\ my_module\ MyViewsStyle
class\ drupal\ MyModule\ Views\ MyStyle
load more v

Other "class-aware" queries related to "Is there a namespace aware alternative to PHP's class_exists()?"