Tag Archives: abstract

PHP MySQL Shopping Cart Tutorial in PHP

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

Very good & useful tutorial that i found while search the web about
Abstracting of shopping cart….

“Yes, this is a another shopping cart tutorial. I am planning to make this tutorial to cover a more sophisticated shopping cart solution but for now it only explains a basic shopping cart. I will improve it in time so stay tuned.”

Check if out here: http://www.phpwebcommerce.com/

Have fun,
Garry Lachman

UF4W22PSSX9C

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

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