Module Object Oriented Programming API

litwol - October 28, 2008 - 03:22

R&D module.

If you can come up with any idea how this module could be useful please do let me know.

This module allows you to write modules in pure OOP syntax.
The only requirement is that oop syntax module must implement hook_boot() like so:

function hook_boot(){
  module_invoke('moopapi', 'register', '[YOUR CLASS NAME WHICH SHOULD BE THE MODULE NAME]');
}

This will register your class as module and will then simply work. Just enable it and enjoy.

Demo module to try this out: create ooptest.module and stick this code into it:

Enable both moopapi.module with ooptest.module and then visit either to /oop or /oop-test to see that it works :-D

ooptest.info:

; $Id$
name = OOPTest
description = The Next Level $417
core = 6.x
version = VERSION

ooptest.module:
<?php
//$Id$


class ooptest {

  public function
menu() {
   
$items = array();
   
$items['oop-test'] = array (
     
'title'            => 'OOP Page Test',
     
'page callback'    => 'ooptest_fark',
     
'access arguments' => array('access content'),
     
'type'             => MENU_NORMAL_ITEM,
    );
   
$items['oop'] = array (
     
'title'            => 'OOP FAPI Test',
     
'page callback'    => 'drupal_get_form',
     
'page arguments'   => array('ooptest_form'),
     
'access arguments' => array('access content'),
     
'type'             => MENU_NORMAL_ITEM,
    );
    return
$items;
  }
  public function
boot() {

  }
  public function
form_alter(&$form, $form_state, $form_id) {
    if (
'ooptest_form' == $form_id) {
     
$form['additional'] = array(
       
'#type' => 'radios',
       
'#title' => t('Is this cool or what!?!?!? - (hook_form_alter() item)'),
       
'#options' => array(
         
'Super cool!',
         
'Hell yeah!!!!',
        ),
       
'#default_value' => variable_get('additional', null),
       
'#weight' => -1,
      );
    }
  }
  public function
form() {
   
$form = array();
   
$form['testing'] = array(
     
'#type'  => 'textfield',
     
'#title'  => t('This is a test field (form item)'),
     
'#default_value' => variable_get('testing', '134'),

    );

    return
system_settings_form($form);
  }
  public function
fark () {
    return
'hello world 2.0 style';
  }
}

/**
* Implementing hook_boot() is a must, to force drupal to load this module as early as possible
* Druring hook_init phase moopapi_init() will initialize all the oop method wrappers and execute hook oop->boot()
*
*/
function ooptest_boot(){
 
module_invoke('moopapi', 'register', 'ooptest');
}
?>

Releases

Official releasesDateSizeLinksStatus
6.x-1.02008-Oct-287.52 KBRecommended for 6.xThis is currently the recommended release for 6.x.
Development snapshotsDateSizeLinksStatus
6.x-1.x-dev2008-Oct-287.53 KBDevelopment snapshotDevelopment snapshots are automatically regenerated and their contents can frequently change, so they are not recommended for production use.


 
 

Drupal is a registered trademark of Dries Buytaert.