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
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
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.”
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
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
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
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 |
becomes
1 |
dont forget “/ /” between the regexp code!!!
Same for “ereg_replace”
1 |
becomes
1 |
Thanks,
Garry Lachman
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" ?> |
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
Using Pear Auth Package for user loging – PHP
Hey…
I think every PHP developer knows the PEAR Package, Its give excellent, flexibility and secured solutions.
One of the is PEAR::Auth.
The PEAR::Auth package provides methods for creating an authenticationsystem using PHP code.
More info about PEAR::Auth you can find here…
Fist of all create mysql db & table:
2
3
4
5
6
7
`id` mediumint(4) NOT NULL auto_increment,
`email` char(100) NOT NULL default '',
`password` char(32) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `email` (`email`)
) TYPE=MyISAM
The Let load the PEAR::Auth
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
// Setup the DNS and Fields name in table 'auth'
$options = array(
'dsn' => "mysql://root@localhost/dbname",
'table' => "auth" ,
'usernamecol' => "email" ,
'passwordcol' => "password",
'db_fields' => array('id','email')
);
// Create instance of PEAR::Auth with the option and the login form function
$a = new Auth("MDB", $options, "loginFunction");
// The function that show the login form
function loginFunction($username = null, $status = null, &$auth)
{
/*
* Change the HTML output so that it fits to your
* application.
*/
echo "<form action=\"".$_SERVER['PHP_SELF']."?login=1\" method=\"post\">";
echo "<input name=\"email\" type=\"text\" />";
echo "<input name=\"password\" type=\"password\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}
// Start the PEAR::Auth Instance
$a->start();
Now when we want to check if the user is connected to the system we do:
2
3
4
5
if ($a->checkAuth()) {
// Getting the fields from the user `auth` table
$_SESSION['userid']=$a->getAuthData('id');
}
For Logout use:
Add users:
PEAR
Have Fun,
Garry Lachman