It can be done more easy:
require_once $someToken'.php';
$last_class = array_pop(get_declared_classes());
*edit:
As written in the comments on this post, this can bring up a notice.
This can be prevented by using one more line:
$all_classes = get_declared_classes();
$last_class = array_pop($all_classes);
thank for the hint.
Of course you can also use end() instead of array_pop(). This should result in the same.
As an alternative you could use this snippet to get the last declared class:
require_once $someToken'.php';
$all_classes = get_declared_classes();
$all_classes_rev = array_reverse($all_classes);
$last_class = $all_classes_rev[0];
which is some kind less elegant.
Back to the first approach:
array_pop(get_declared_classes()) brings you the last declared class always
of course this does not include the case you define multiple classes before calling array_pop(get_declared_classes())
so the above line must be used every time you include a new class what makes it not being "D.R.Y". You should perhaps therefore consider another way of solving that problem.
This is a complete different way which would be more elegant:
Create a Helper class that you can use to register new classes:
/* the helper: */
class MyDefinedClasses {
$classes = array();
public static function register_class($class){
self::$classes[] = $class
}
public static function get_registered_classes(){
return self::$classes;
}
}
/*helper ends here*/
here one class as example of these you include and write your self:
/*misc function class to be registered*/
class MyFunctions {
public ....
}
myDefinedClasses::register_class(new MyFunctions()); // this is where the alternative approach of solving your problem kicks in
/*(php)file ends here*/
now you are always able to get all your custom and registered classes by calling myDefinedClasses::get_registered_classes();
and the best about it: you directly got a class instance without using php's reflection functionality