[#] Rozszerzenie Drupala o własne klasy php

( Ostatnio zmieniony ndz., 29/06/2008 - 01:15 )
 

Moduł Drupala

przygotowany do obsługi zewnętrznej klasy php

<?php
// $Id:

/**
 * @file
 * main file example module
 */


/**
 * Implementation of hook_menu().
 */
function example_menu() {

    $items = array();

    $items['testowy'] = array(
      'title'            => 'Example',
      'description'      => 'Example description',
      'page callback'    => 'example_page',
      'access arguments' => array('administer site'),
    );

  return $items;
}


/**
 * Implementation of example_page().
 */
function example_page() {
  // start oea
  $output = _start_mybox();


  return $output;
}



/**
 * Main start_mybox
 */
function _start_mybox() {

  $mybox =& _example_get_mybox();

  if ($mybox) {
    $out = '';
    $out = $mybox->sample();
    return $out;
  }

  return '';
}


/**
 * Initialization of mybox
 */
function _example_get_mybox($messages = FALSE) {

  // check for PHP5 (also requires iconv)
  if (version_compare('5.1.0', PHP_VERSION, '<=') && function_exists('iconv')) {

    require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'mybox.class.php');

    // create new mybox object
    //$mybox = new MyBox;
    $mybox = MyBox::getInstance();

    return $mybox;

  }
  else {
    drupal_set_message(t('You have PHP4!'), 'warning');
    return false;
  }

}

Klasa PHP

przykładowa konstrukcja klasy php

<?php

/**
 * MyBox class
 *
 */
class MyBox {

  private static $instance;


  /*
   * Default public constructor
   */
  private function __construct()
  {
    if (method_exists($this, '__destruct')) {
      register_shutdown_function(array(&$this, '__destruct'));
    }
  }

  //function __destruct() {}

  /*
   * Disable clone object
   */
  private function __clone() {}

  /*
   * Create object if not exist
   */
  function getInstance()
  {
    return ($i = &self::$instance) ? $i : $i = new self;
  }

/**************** start own method *******************/

  function sample()
  {
    return 'This is sample method';
  }

}
?>
3.88889
Twoja ocena: Brak Średnio: 3.9 (9 głosów)