Tag Archives: pattern

CodeIgniter – PHP 5 MVC Framework

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

I really never used frameworks, i always build my system without any frameworks.
but no more… after few days with CodeIgniter i`m in Love…

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks”

CodeIgniter

Have a nice day ;)

Garry Lachman

Share

Factory Design Pattern in PHP5

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

Factory Method return you the specific object by params he gets.
Lets make an example… we got 2 different classes for students that extends StudentAbstract class,
now we can make factory class that return the class by param.

StudentAbstract.php

1
2
3
4
abstract class StudentAbstract {
    abstract function getFirstName();
    abstract function getLastName();
}

FirstGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class FirstGradeStudent extends StudentAbstract {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Garry";
        $this->lastName = "Lachman";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

SecondGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class SecondGradeStudent extends StudentAbstract    {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Polani";
        $this->lastName = "Almoni";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

GetStudentFactory.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include_once("FirstGradeStudent.php");
include_once("SecondGradeStudent.php");

class GetStudentFactory {
    public function getStudent($grade)  {
        $student = NULL;
        switch ($grade) {
            case "first":
                $student = new FirstGradeStudent();
            break;

            case "second";
                $student = new SecondGradeStudent();
            break;

            default:
                $student = new FirstGradeStudent();
            break;
        }
        return $student;
    }
}

Test.php

1
2
3
4
5
6
7
8
9
include_once("GetStudentFactory.php");

$factory = new GetStudentFactory();

$firstGrade = $factory->getStudent("first");
// return FirstGradeStudent class

$secondGrade = $factory->getStudent("second");
// return SecondGradeStudent class

Have Fun ;)
Garry Lachman

Share

Doru Moisa’s Blog: Static call versus Singleton call in PHP

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

New blog post by Doru Moisa about Static call vs. Singleton… very recommended!!!

“In the past several months I’ve been working with a rather large application built with symfony. I noticed that symfony makes heavy use of the Singleton pattern (other frameworks, like Zend do that too)…”

http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/

Enjoy ;)
Garry Lachman

Share

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 MVC in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3, Flex. Tagged , , , , , , , , , , , . .

I always used to apply this pattern in most simple way.
Its help me to create few view states use same functionality.

First i create interfaces to Module, Controller and View state:

1
2
3
4
5
6
//Module Interface
package {
flash.events.IEventDispatcher;
public interface IModule extends IEventDispatcher {
}
}
1
2
3
4
5
//Controller Interface
package {
public interface IController {
}
}
1
2
3
4
5
//View Interface
package {
public interface IView {
}
}

Than lets create the MVC:

1
2
3
4
5
6
7
8
9
//Module
package {
flash.events.EventDispatcher;

public class TestModule extends EventDispatcher implements IModule {
public function TestModule(){
}
}
}
1
2
3
4
5
6
7
8
9
10
//Controller
package {
public class TestController implements IController {
private var testModule:IModule;

public function TestController(_module:IModule){
testModule = _module;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//View
package {
import flash.display.Sprite;

public class TestView extends Sprite implements IView {
private var testModule:IModule;
private var testController:IController;

public function TestController(_controller:IController, _module:IModule){
testController = _controller;
testModule = _module;
}
}
}

Now lets connect them all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Test MVC
package {
import flash.display.Sprite;

public class TestApp extends Sprite {

public function TestApp(){
var testModule:IModule = new TestModule();
var testController:IController = new TestControler(testModule);
var testView:IView = new TestView(testController, testModule);
addChild(testView as Sprite);
}
}
}

Share

Singleton pattern in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , . .

Singleton pattern in ActionScript 3

the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system.

Useing of singleton in AS3 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package
{

public class as3Singleton
{

public function as3Singleton(_e:Enforcer)
{

}

/**
*   singleton static instance holder
*/

private static var instance:as3Singleton;

/**
*
* @return as3Singleton Instance (Signleton)
*
*/

public static function getInstance():as3Singleton
{
if (instance == null)
instance = new as3Singleton(new Enforcer(), _stage);

return instance;
}
}

}
class Enforcer{public function Enforcer(){}}
Share