0

I want to dynamically declare classes (= require .php files), but I need to know, which class was actually defined (all I need is name). My current solution is:

$classesBefore = get_declared_classes();
require_once $someToken'.php'; // it's actualy more complex than this...
$classesAfter = get_declared_classes();

$classesDiff = array_diff($classesAfter, $classesBefore);
$declaredClass = end($classesDiff);

echo $declaredClass; // here is name of currently declared class

Can this be done more efficient way? And if not, can I be sure that last defined class will be last at defined classes list (because of $declaredClass = end($classesDiff);)?

i.am.michiel
  • 10,281
  • 7
  • 50
  • 86
Pavel Štěrba
  • 2,822
  • 2
  • 28
  • 50

2 Answers2

0

There is another post here with alternate solutions to do what you're looking for. Here's the snippet from that post:

function file_get_php_classes($filepath) {
  $php_code = file_get_contents($filepath);
  $classes = get_php_classes($php_code);
  return $classes;
}

function get_php_classes($php_code) {
  $classes = array();
  $tokens = token_get_all($php_code);
  $count = count($tokens);
  for ($i = 2; $i < $count; $i++) {
    if (   $tokens[$i - 2][0] == T_CLASS
        && $tokens[$i - 1][0] == T_WHITESPACE
        && $tokens[$i][0] == T_STRING) {

        $class_name = $tokens[$i][1];
        $classes[] = $class_name;
    }
  }
  return $classes;
}
Community
  • 1
  • 1
  • 3
    Instead of marking it as a possible duplicate or write a comment you just going to copy the entire code from the other post? This doesn't serve anybody! – Rizier123 Feb 19 '15 at 08:28
0

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

serjoscha
  • 510
  • 4
  • 10