Developers Archive for December, 2006

Fluent Interfaces in PHP5

Fluent Interfaces in PHP5 Friday, December 29th, 2006

In PHP 5 terms, a fluent interface to an object is one where the setter methods return an object handle. It is perhaps simplest to always return $this, however any object handle can be returned. Here’s a simple PHP class that demonstrates how a fluent interface is built:

<?php
class Fluent {
public function hello() {
echo ‘hello ‘;
return $this;
}

public function world() {
echo ‘world’;
return $this;
}
}
$fluent = new Fluent();
$fluent->hello()
->world();
?>

The code above will output hello world. As you can see, a fluent interface is quite easy to implement.
This buzzword fluent interfaces is a way of chaining methods of an object together. By having a method return a reference to the object itself, return $this; you chain methods together like this $this->methodOne()->methodTwo()->methodThree();. This can make your code easier to read, and that is the point of using fluent interfaces, making your code easier to read.

The first example is a snippet from a class in which the method makeNormal() is used to create an order in the system. The makeNormal() method is part of the order object.

<?php
private function makeNormal(Customer $customer) {
$o1 = new Order();
$customer->addOrder($o1);
$line1 = new OrderLine(6, Product::find('TAL'));
$o1->addLine($line1);
$line2 = new OrderLine(5, Product::find('HPK'));
$o1->addLine($line2);
$line3 = new OrderLine(3, Product::find('LGV'));
$o1->addLine($line3);
$line2->setSkippable(true);
$o1->setRush(true);
}
?>

Next, they show the fluent interface version of the code.

<?php
private function makeFluent(Customer $customer) {
$customer->newOrder()
->with(6, 'TAL')
->with(5, 'HPK')->skippable()
->with(3, 'LGV')
->priorityRush();
}
?>

As you can see, the second example is easier to read, assuming you understand that the with() method adds lines to the order. As stated earlier, this bit of magic is accomplished by the with(), skippable(), and priorityRush() methods retuning a reference to the object. ($this) That is the one and only secret to fluent interfaces.

Ereg usage - Checking alpha numeric characters

Ereg usage - Checking alpha numeric characters Friday, December 29th, 2006

Description

This verifies if a PHP variable string contains characters other than letters or numbers using the PHP function ereg. This PHP code snippet can be useful for form input where you only want users to input alpha numeric characters.

Example

<?php

// Example 1

$text = “onlyalphanumericcharacters012345″;

if (ereg(’[^A-Za-z0-9]’, $text)) {
echo “This contains characters other than letters and numbers”;
}
else {
echo “This contains only letters and numbers”;
}

// Example 2

$text = “mixedcharacters012345&../@”;

if (ereg(’[^A-Za-z0-9]’, $text)) {
echo “This contains characters other than letters and numbers”;
}
else {
echo “This contains only letters and numbers”;
}

?>

 

mysql_escape_string Function

mysql_escape_string Function Friday, December 29th, 2006

mysql_escape_string Function:
Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection.

Syntax:
string mysql_real_escape_string ( string unescaped_string [, resource link_identifier])

The main advantage of the mysql_real_escape_string() over addslashes() is, mysql_real_escape_string() takes character set into account and thus it predetermine how to escape the data.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.