Home » Php » php – Understanding reflection classes

php – Understanding reflection classes

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to understand how reflection classes work, I am unable to find a good explanation of one and examples, but also a little help with my own issue!

So, first off my issue:

When making a new reflectionClass I cannot seem to get it to work and use the class.

Here is my code:

$class = new ReflectionClass("Feature\CollectionTesting\CollectionTest");

And from what I read from the documentation you are meant to pass it a class name (but mines not in the same directory or in the same file).

This is obviously wrong as it throws me an error:

Uncaught ReflectionException: Class Feature\CollectionTesting\CollectionTest does not exist in (my current file)

With this I was also told I would need to load the classes into the file to be able to use them so I have done like so:

spl_autoload_register(function ($finalPath) {
                include $finalPath;
            });

Where $finalPath is the path to the file itself. For example it would be something like:

Feature\CollectionTesting\CollectionTest.class.php

Is someone able to explain to me how they work and provide examples, plus help me with my issue or help me understand what the issue is so I can get this working?

How to&Answers: