1

am writing a small set of framework structures for PHP (not be confused with a MVC style framework) and I would to like to validate my approach because it seems very "clumsy":

All requests are piped to a bootstraper through redirect directive (apache htaccess).
All necessary files are required by the bootstrapper.
Then user classes are called based on routes (this works fine, not included here)
Each user module extends core module, that in turn instantiates support classes like DB and CACHE, thus inherits the instances and I don't have to care about opening/managing connections or if I instantiate other classes within those classes - I don't have to pass my db/cache as arguments. Also, if I have a large request with plenty of user class methods calls within the same request - I think it would only use one instance of the core.module - which should be a good thing. Right? Right?

However, the config passing seems to be iffy: I hate GLOBALS with passion - BUT I cant make a static config class because I can not put the array there (no static arrays in PHP). Also, this breaks all the IDE intellisenses and the devs are mad at me :(

Does the whole approach make sense? Should I go RTM more?

Thank you for your honest opinions, the pseudo-code is attached below.

//my_config_prod.php
<?php
      $CONFIG = array('db' => array(..multi-level array here..), 'cache' => array(...));
?>

//bootstraper.php
<?php
      require '../config/my_config_prod.php';
      require '../core/core.mysql.php';
      require '../core/core.cache.php';
      require '../core/core.module.php';
      require '../class/class.user.php';

      //initialize things:
      $GLOBALS['DB'] = new CoreMysql($CONFIG['db']);
      $GLOBALS['CACHE'] = new CoreCache($CONFIG['cache']);

      //run, this is simplified - route handler is installed here that will 
      //call appropriate module based on the incoming request

      $user = new User();
      $info = $user->load_user(1);
 ?>  

 //core.module.php
 <?php
       public function __construct () {
            global $CACHE;
            $this->cache = $CACHE;

            global $DB;
            $this->db = $DB;
       }
 ?>

 //class.user.php
 <?php
       class User extends CoreModule{

           public function __construct() {
             parent::__construct();
           }

           public function load_user($id) {
             return $this->db->get_user($id)
           }
       }
  ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Terry Felkrow
  • 653
  • 1
  • 8
  • 18

2 Answers2

0

You can use phpDoc to add intellisense manually in many IDEs. Have you considered using a DIC and/or class autoloader for shared resources? You shouldn't have to include everything for every page request.

http://php.net/manual/en/language.oop5.autoload.php

http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.method.pkg.html

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • The main reason I stay away from autoloading is because APC (or any op-code cache) just doesn't work with spl-autoload or any conditional loads. In my experience the benefits of op-cached "everything" outweighs autoloaded flow, just because in my case almost all modules end up being loaded. – Terry Felkrow Oct 27 '11 at 17:32
  • @Terry Felkrow http://stackoverflow.com/questions/1941541/php5-frameworks-autoloading-and-opcode-caching - autoloading does not prevent compile time binding. objects created within conditions, or when different definitions are created depending on conditions are what cause compile time binding issues. – dqhendricks Oct 27 '11 at 23:21
  • @Terry Felkrow to clarify autoloading does not prevent the script containing the class from caching. actual instances of objects in many situations however may not be cached, but this is to be expected in almost all applications to some degree and doesn't have much to do with autoloading. this is my understanding of the subject at least. – dqhendricks Oct 27 '11 at 23:34
  • also, you may want to look into http://php.net/manual/en/function.parse-ini-file.php for storing config info in external ini files. – dqhendricks Oct 27 '11 at 23:37
  • Regarding APC+autoload: http://stackoverflow.com/questions/1396501/do-php-opcode-cache-work-with-autoload and http://stackoverflow.com/questions/1941541/php5-frameworks-autoloading-and-opcode-caching – Terry Felkrow Oct 28 '11 at 00:09
  • @Terry Felkrow Yep. I believe those reiterate what I just said. – dqhendricks Oct 28 '11 at 02:46
0

Your globals are kind of registry pattern. You may store the config in object instead of the array, this would be a better approach.

For the best reference, take a look at Zend Framework 2 configs, bootstraps and application resources.

takeshin
  • 49,108
  • 32
  • 120
  • 164