Tag Archives: php5

Migrating from PHP 4 to PHP 5 Presentation

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

Migrating from PHP 4 to PHP 5 by Derick Rethans.
Really good one, just give a look…

http://talks.php.net/show/migrating-lt2005

Have Fun ;)
Garry Lachman

Share

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

CONFIRMED: Facebook Gets Faster, Debuts Homegrown PHP Compiler

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

Mike Melanson write on readwriteweb.com “The rumors have been flying over what’s going on over at Facebook headquarters. The word has been that a PHP team was brought in and made to sign non-disclosure agreements before discussing a PHP project that has been in development for the past two years. Alex Handy, senior editor of the Software Development Times Blog, predicted last Saturday that Facebook “has rewritten the PHP runtime from scratch,” and several sources have confirmed for us tonight that Facebook has indeed been making some changes to the basic PHP runtime environment.

According to our sources, Facebook has been working on a PHP compiler that will
increase speed by around 80% and offer a just-in-time (JIT) compilation engine that will offer a number of advantages. The project is very similar to Google’s Unladen Swallow project, which rebuilt the Python compiler, boosting the speed fivefold and opening the door for multi-language integration.”

More on readwriteweb.com

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

Adapter Design Pattern in PHP5

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

Adapter patter is very simply pattern that make an interface
for another class using his functions

ExampleItem.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class ExampleItem   {
    private $name;
    private $title;

    private fucntion __construct($name, $title) {
        $this->name = $name;
        $this->title = $title;
    }

    public function getName()   {
        return $this->name;
    }

    public function getTitle()  {
        return $this->title;
    }
}
?>

ExampleItemAdapter.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include("ExampleItem.php");

class ExampleItemAdapter    {
    private $exampleItem;

    private fucntion __construct(ExampleItem $exampleItem)  {
        $this->exampleItem = $exampleItem;
    }

    public function getItemInfo()   {
        return $this->exampleItem->getName() . " - " . $this->exampleItem->getTitle();
    }
}
?>

testAdapter.php

1
2
3
4
5
6
7
8
9
<?php
include("ExampleItem.php");
include("ExampleItemAdapter.php");

$test = new ExampleItemAdapter(new ExampleItem("Garry Lachman","SomeTitle"));

echo $test->getItemInfo();
// result: "Garry Lachman - SomeTitle"
?>

Have Fun ;)
Garry Lachman

Share

“ereg is deprecated” warnings in PHP 5.3

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

If you try to install systems like OSCommerce on PHP 5.3.x you may got strange
error like “ereg is deprecated…”.

Here is the solution!!!

1
ereg('RegExp', $x $y);

becomes

1
preg_match('/RegExp/', $x $y);

dont forget “/ /” between the regexp code!!!

Same for “ereg_replace”

1
ereg_replace('RegExp', $x, $y);

becomes

1
preg_replace('/RegExp/', $x, $y);

Thanks,
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 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