Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, is there a way to get all declared classes in a specific namespace?

I'd like to get the names of all classes within a specific namespace in PHP. Currently, I'm attempting to do some magic via reflection on a specific list of named classes, but I'd like to do it without knowing the names ahead of time.

I've tried calling get_declared_classes(), but for whatever reason, classes that I do have available are not showing up. I can call get_declared_classes(), not see Event in the list, then immediately call $x = new Event() without a problem. Something like the following, which I would think should cause a problem...

if (! in_array('Event', get_declared_classes())) { $x = new Event(); }

...works fine. I'm wondering if namespacing these classes and retrieving that way would help alleviate the problem. Is this possible?

EDIT: For clarification, let me add that I am not currently using namespaces, and I am not specifically trying to achieve something from the above listed code. What I want is to get the names of all classes I have declared. Despite the fact the class declarations for all of them are being hit before I call get_declared_classes(), they are not all appearing in the list. I was hoping that namespacing might help solve the problem.

EDIT2: Several people have pointed out that the classes may be autoloaded. I tested this by doing the following. echo(class_exists('Event')) returned a value of 1. echo(class_exists('Event', FALSE)) returned a value of 0. The second, optional parameter to class_exists is whether or not to autoload. So, apparently the class is being autoloaded. That answers that.

So, next question - how do I prevent this? I'm using a framework that really doesn't give me much low-level control. Is there a way to force autoloading, THEN call get_declared_classes, or for get_declared_classes to fire an autoload first?

like image 343
rybosome Avatar asked Dec 16 '11 00:12

rybosome


People also ask

Can we use two namespaces in PHP?

Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.

What is Namespacing in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

Can a namespace have the same name as a class?

Inside a namespace, no two classes can have the same name.

What is PHP namespace class?

A namespace is a hierarchically labeled code block holding a regular PHP code. A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants. Namespaces are declared using the namespace keyword.


1 Answers

You do not need to hard code it in the code, you can use variable name:

$class_name = 'Event';
if (!in_array($class_name, get_declared_classes())) {
    $x = new $class_name();
};

See similar code in action here: codepad.org/hCLE4ToA.

Also some classes may not appear in get_declared_classes()'s result, because they may not be loaded at the time this function is called. It may be the case if they are autoloaded after you try to instantiate them. See more on autoloading classes here: php.net/autoload.

Does it answer some of your questions? Did it help?

like image 133
Tadeck Avatar answered Sep 29 '22 08:09

Tadeck