Re: php5 oop question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've seen it referred to as a "Fluent Interface". I built one just to see how hard it was, using a standard problem: data validation. The results were promising.

I combined it with an object designed to work as a factory for an internally stored decorator pattern.

Class Input was the factory, with a private property to hold the decorator object in question and a method for adding Decorators to the stored object based on a simple "switch" command. Class Checker was the root class of the decorator pattern, doing nothing but returning TRUE on its check() method. Decorator Classes for Checker included IntegerChecker, NumericChecker, NonEmptyChecker, RegExpChecker, EmailChecker, etc. etc. All of which extended Checker, have their own custom check() method and a property to store the Decorator object which they wrap.

Each Checker applies its check(X) method first, then passing a successful request along to the Decorator object they store. Input has a check() method that just passes the request along to its stored Decorator object.

By using the Fluent style of interface, the end result was

<?php
$_input = new Input();

$_input->addCheck('Integer')
	->addCheck('Range',3,9)
	->addCheck('NonEmpty');

echo ($input->check('4.734'))? 'Pass': 'Fail';
?>

It returns 'Fail'. Why? It passed the NonEmpty test, passed the Range test, failed the Integer test.

print_r of $input object:
Input Object
(
	[_checker:private] => NonEmptyChecker Object
		(
			[_checker:private] => RangeChecker Object
				(
					[_checker:private] => IntegerChecker Object
						(
							[_checker:private] => Checker Object
								(
								)
						)
					[_min] => 3
					[_max] => 9
				)
		)
)

Validation works from the outside in, responses are passed from the inside out, so on first failure, the validation process bails.

I loop through an array of prepared Input objects for validating the supplied Post values from a form and get back a simple pass/fail response for each.

A bit of work to set up initially, but once the system is built, it's pretty elegant.

Tim Stiles
Icomex.com,
DallasPHP.org


On Apr 12, 2007, at 1:29 AM, Paul Scott wrote:


On Wed, 2007-04-11 at 23:22 -0700, Jim Lucas wrote:

Has anybody else seen this style of syntax?

http://5ive.uwc.ac.za/index.php? module=blog&action=viewsingle&postid=init_8059_1163957717&userid=57290 61010

I don't think that its really useful for anything, except maybe creating
overly complex SQL queries.

--Paul

All Email originating from UWC is covered by disclaimer http:// www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Tim Stiles,
WatchMaker,
Icomex.com



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux