Monthly Archives: February 2010

Singleton Pattern in PHP5

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , . .

Singleton is very basic design pattern.
If the instance is inited once than the class return the same instance.

Example of signleton in php5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
Class SingletonExample {
  static private $instance;

  private function __construct() {
  }

  static function getInstance() {
    if(!Self::$instance) {
      Self::$instance = new SingletonExample();
    } else {
      return Self::$instance
    }
  }

  public function doHello() {
     return "Hello World";
   }
?>

Usage Example:

1
2
3
4
5
6
<?
include_once("singleton.class.php");

$single = SingletonExample::getInstance();
echo $single->doHello(); //return "Hello World"
?>
Share

Simple Modules Engine in PHP5

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , . .

I write a really simple module enegine in php5… very easy and nice…

Module Abstract:

1
2
3
4
5
6
7
8
9
<?php
abstract class baseModuleAbstract   {
    protected $moduleName;
       
    protected function setModule($name) {
        $this->moduleName = $name;
    }  
}
?>

Module (mainModule):

1
2
3
4
5
6
7
8
<?php
class mainModule extends baseModuleAbstract
{
    public function __construct(){
        $this->setModule("mainModule");
    }  
}
?>

index.php?module=mainModule

1
2
3
4
5
$module = $_GET['module'];
if (isset($module)) {
    include("modules/" . $module . ".module.php");
}
$loadedModule = new $module();

Thats it…
Its only a example and there is many security issues to need close

Have a nice day :)
Garry Lachman

Share