PHP sınıfı olay uzatıldı

0 Cevap php

İşte (bu yazı boyunca bu akılda tutmak) konuşurken olacak kod:

Dosya: index.php

/**
 * Base class used when created a new application.
 */
class App {
     public function do_something(){
     }
}

/**
 * Class used, among other things, to manage all apps.
 */
class Apps {
    public static function _init(){
        foreach(glob('apps/*') as $dir)
            if(file_exists($dir.'/index.php')
                include_once($dir.'/index.php');
    }
}
Apps::_init();

Dosya: Uygulamam / index.php

class MyApp extends App {
     /**
      * This function overrides the the one in App...
      */
     public function do_something(){
     }
}

Yani, muhtemelen ben ne yapıyorum biliyor musun; bir uygulama / uzantısı sistem, uygulama /apps/ ayrı bir klasörde tutulur ve o giriş noktası index.php.

The code, so far, works nicely (or, well it should, I wrote it off the top of my head ;)). Anyway, my issue is to make Apps class aware of all the extended App classes.


Kolay yolu, her uygulamanın index.php sonunda aşağıdaki gibi bir şey yazmak olacaktır.

Apps::register('MyApp'); // for MyApp application

The problem with it is that although it's understandable, it is not automated. For instance, copy+pasting an application needs that modification, and new developers are more likely to completely forget that code (and even worse, most of the code still works without it!).

Başka bir fikir de kodundan sonra bu kodu kullanmak oldu _init():

$apps=array();
foreach(get_declared_classes() as $class)
    if(array_search('App',class_parents($class))!==false)
        $apps[]=$class;

Ama çok kaynak-yoğun sonuncusu sesler.

Sen ne düşünüyorsun?

0 Cevap