PHP-MySql Universal Connection: The Try/Catch Exception Tweak

universal2In 2012, I posted a little piece on a PHP-MySql connection utility. Since the time of the original posting, I’ve used that utility again and again. It’s been used in my own applications (many of which have appeared on this blog), with my clients, and with others who I was able to help. Other than minor tweaking with the replacement of a conditional if statement with the try/catch exception, it has remained largely unchanged.

Recently, John Slegers from Belgium provided some thoughtful comments for refactoring the little utility. (Being a big Salamander fan [Netflix], I listen to what the PHP folks from Belgium have to say!) His comments inspired some re-thinking, and while I made some changes, they were minor and along different lines. Download the revised version and take a look at it:
Download

Object Thinking

In developing software, I think in terms of objects and object structures. That is, rather than thinking in lines of code and statements, I find it easier to think in terms of what an object does and how it fits into a larger object structure rather than how statements are used. So, in using PHP with MySql, I tend to view connections to the database as something an object handles and I don’t need to worry about otherwise. Recently I was working on an Observer design patter using the built-in SplSubject and SplObserver. It occurred to me that the concrete subject might pull data out of a MySql database, and so I just popped a copy of the universal connect utility in the folder and it’s all set. Figure 1 illustrates it use:

Figure 1: Adding connection utility to small observer

Figure 1: Adding connection utility to small observer

The utility is very small—a single interface and class—but it’s very useful.

The Code

First of all, an interface stores connection information in constants, and as long as you’re using the same database, you can re-use the values. The single method is defined as public and static so that a single line of code can launch the method that uses the constants for connection. It is largely unchanged from the original.

<?php
//Filename: IConnectInfo.php
interface IConnectInfo
{
	const HOST ="localhost";
	const UNAME ="bart";
	const PW ="itchy";
	const DBNAME = "scratchy";
 
	public static function doConnect();
}
?>

The UniversalConnect class does all of the work for making a connection using the constants in the interface. Again, static declarations mean that the class need not be instantiated to use the method:

<?php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
include_once('IConnectInfo.php');
 
class UniversalConnect implements IConnectInfo
{
	private static $server=IConnectInfo::HOST;
	private static $currentDB= IConnectInfo::DBNAME;
	private static $user= IConnectInfo::UNAME;
	private static $pass= IConnectInfo::PW;
	private static $hookup;
 
	public static function doConnect()
	{
		self::$hookup=mysqli_connect(self::$server, self::$user, self::$pass, self::$currentDB);
		try
		{	
			self::$hookup;
			//Comment out / Uncomment following line for develop/debug
			echo "Successful MySql connection:<br />";
		}
		catch (Exception $e)
		{
			echo "There is a problem: " . $e->getMessage();
			exit();
		}
		return self::$hookup;
	}
}
?>

The try/catch exception stops everything cold if the connection is not made, and the Exception provides the information you need to correct it. If the try is successful, it returns the connection to the client.

That’s pretty much it. During development, I like to see the Successful MySql connection message, but once everything is ready to go, I just comment it out. The following little class tests the connection and should not be included in a development folder:

<?php
include_once('UniversalConnect.php');
class UseMySQLConnect
{
	private $doConn;
 
	public function __construct()
	{
		try
		{
			$this->doConn=UniversalConnect::doConnect();
			echo "Succesful connection";
			$this->doConn->close();
		}
		catch (Exception $e)
		{
			echo "There is a problem: " . $e->getMessage();
			exit();
		}
	}
}
$worker=new UseMySQLConnect();
?>

The line,

$this->doConn=UniversalConnect::doConnect();

takes care of all connection steps for you, and instead of having your code piled up with the same code block required by mysqli() (PHP’s improved MySql function), you just make sure the include_once makes the UniversalConnect class available, and you can use it wherever you need MySql.

Share

PHP Visitor Design Pattern III: Traverser

phper250After going through single and double dispatch, it’s time to shift gears to the next major feature of the Visitor pattern; the Traverser. One of the most important (and often omitted) components of the Visitor design pattern is the Object Structure. In Part II the focus is on the double-dispatch, the number of elements that could be visited at once. The Object Structure is set up to collect the allowable visitors to visit the elements; however, only one element at at time can be displayed because the middle of the the foreach loop in the confirm() method contains a return statement—so as soon a the iteration begins, it ends with the return statement. (Let’s face it; it’s a pseudo-transveral.) Since Part II focuses on the double-dispatch, the number of elements that could be visited at one time is a tangental issue. This post, though, focuses squarely on the traversal process and the multiple elements that can be visited in a single process.

Updating the Visitor

In order to have all of the parts merrily humming along, a few changes are in order. Importantly, once these changes are made, all of the parts are still working and interacting together. To see what’s changed but how the structure is basically the same as the visitor in Part II focuses on the double-dispatch, the number of elements that could be visited at on, download the new set of files and test the new multiple element visitor:
PlayDownload
Right away, you will see that instead of radio buttons, the HTML5 UI uses check boxes for selecting one, two or three elements and then applies the visitors one at a time.(You can supply your own error-handling routine for selecting no elements!)

The HTML Element Array

One of the nicest attributes in HTML is the ability to treat a group of checkboxes with “element values” (elements as elements in an array). Each checkbox is given a name with empty brackets (e.g., my element[]). All of the selected (checked) checkboxes with the identical element name are automatically included in an array with the element name. You don’t have to worry about the unchecked ones as they are not included in the array passed to PHP.

< !DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="visitor.css"/>
    <title>The Traverser</title>
</head>
<body>
    <h3>Traverser: Multiple Elements & Visitor Color</h3>
    <div id="choose">
        <div id="geom">
    <form action="Client.php" method = "post" target = "feedback">
        <input type="checkbox" name="shape[]" value="Circle" checked="True"/>&nbsp;Dispatch a circle<br />
        <input type="checkbox" name="shape[]" value="Square"/>&nbsp;Dispatch a square<br />
        <input type="checkbox" name="shape[]" value="Triangle"/>&nbsp;Dispatch a triangle<br />
        </form></div>
 
        <aside>
            <fieldset>
                <legend>Color Visitor</legend>
        <input type="radio" name="color" value="GreenVisitor" checked="True"/>&nbsp;Green<br />
        <input type="radio" name="color" value="RedVisitor"/>&nbsp;Red<br />
        <input type="radio" name="color" value="BlueVisitor"/>&nbsp;Blue<br />
        <input type="radio" name="color" value="BurntOrangeVisitor"/>&nbsp;Burnt Orange<br />
        </fieldset>
        </aside>
        </div>
        <p><input type="submit" name="sender" value="Double Dispatch"/></p>
 
    <div>
    <iframe name="feedback" width="680" height="680">feedback</iframe>
    </div>
</body>
</html>

That checkbox array feature made it very easy to pass the shape class names (as strings) to the Client.

The Updated Client

The Client class is changed slightly to accept an array of shapes rather than just one as was done in Parts I and II of the Visitor series. Likewise, some steps were tightened up in the ObjectStructure class for cleaning up the traversal process.

< ?php
//Client.php
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
// Autoload given function name.
function includeAll($className)
{
    include_once($className . '.php');
}
//Register
spl_autoload_register('includeAll');
 
class Client
{
    private static $shapeElement;
    private static $color;
    private static $package;
 
    //client request
    public static function request()
    {
      self::$shapeElement= array();
      self::$shapeElement=$_POST['shape'];
      self::$color=$_POST['color'];
      self::$package= array();
 
      $obStructure = new ObjectStructure();
      $colorVisitor= new self::$color();
 
      //Attach concrete elements to array & accept visitor
      foreach (self::$shapeElement as $shapeNow)
      {
        $obStructure->attach(new $shapeNow,$colorVisitor);
      }
 
      //Display selected shapes
      self::$package=$obStructure->getElements();
      foreach (self::$package as $colorShape)
      {
        echo $colorShape->showShape();
      }
    } 
}
Client::request();
?>

The Client calls on the traversal process using an instance of the OjbectStructure. The attach() method now has two parameters; a shape and a color. The shape is passed as a shape element generated by the foreach loop, creating a new shape with each iteration while adding the same color visitor to all shapes. Once everything has been stored in the ObjectStructure, a second loop calls the getElements() methods, which returns shapes with the appropriate color visitor.

The ObjectStructure

The ObjectStructure class is small but mighty. The Gang of Four note,

A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor.

Several choices are available to transverse the elements from the client through the object structure, but as seen, the humble (yet mighty) foreach loop handles the job elegantly and efficiently. The OjbectStructure provides the methods the client uses for traversal.

< ?php
//ObjectStructure.php
class ObjectStructure
{
    private $elements=array();
 
    public function attach(IElement $element,IVisitor $colorVis)
    {
        $element->accept($colorVis);
        array_push($this->elements,$element);
    }
 
    public function getElements()
    {
        return $this->elements;
    }  
}
?>

In Part II of the Visitor series, the attach() method only served to push the element onto an array, but here the method, now with two instead of one parameter serves double duty. First, each shape accepts (using the IElement::accept() method) the visitor. Second, each shape is added to an array, $elements. In some respects, this acts like a setter operation.

The only other method, getElements() returns the shapes with the accepted color. The client can then display them on the screen.
Continue reading ‘PHP Visitor Design Pattern III: Traverser’

Share

PHP Visitor Design Pattern II: The Double Dispatch

visitor2x

The Visitor pattern uses a double dispatch even with languages that are inherently single dispatch (such as PHP). In this second installment of the Visitor, I’d like to look at the concept and utility of double dispatch and role of the ObjectStructure in creating a working Visitor example that can be transformed into a practical application.

In Part I PHP Visitor Design Pattern I: The Single Dispatch, the focus was on using a single dispatch and a “pretend visitor.” This post shows how to create a visitor object based on both a Visitor interface and ConcreteVisitor implementations that are used in concert with the Element participants via the ObjectStructure. In order to do this, a couple more concrete Elements have been added to the example begun in Part I of the Visitor.

The Visitor Design Pattern Diagram

The Part I Visitor post suggests that the Visitor class diagram is a bit daunting, and so I held off until now to show it. If you look at the bottom portion of Figure 1, you will see that the example in Part I handled all of the Element participants, and included a “pretend visitor” where the Accept(Visitor) implementation in an actual Visitor pattern goes. So, you have some idea of what close to half of the Visitor does.

Figure 1: Visitor Class Diagram

Figure 1: Visitor Class Diagram

You can get a hint at what double-dispatch is by looking at the dual connections that the Client has to both the Visitor and Element (via the ObjectStructure). In the pattern, the Client is implied, but it’s clear to see the double reference to both the Visitor and the ObjectStructure which holds a reference to the Element.

This particular implementation of the Visitor pattern extends the example used in Part I where a “pretend” visitor is an operation that provides the fill color of SVG images. In this implementation, the fill color operation is provided by an actual visitor object. A third and forth Element class have been added, one with a visitor (Triangle) and one that does not have a visitor (zigzag lines have no fill colors—just a stroke color.) Figure 2 shows the program as a file diagram:

Figure 2: File diagram of Visitor pattern in PHP

Figure 2: File diagram of Visitor pattern in PHP

To get an idea of what the application does and look at the overall code, run the program and download the files:
PlayDownload

When you run the program, you can see that the shape (concrete Element) and the color (concrete Visitor) are selected separately. Those separate selections are to highlight the concept of double-dispatch. A shape and color are selected with the understanding that the developer had not included a fill color originally, and instead of starting from scratch to re-program the shape selector, the developer added a visitor. To see how double-dispatch works in a Visitor pattern, follow the path from the Client to the ObjectStructure and to the IElement::accept() method.

Double-Dispatch and Traversing Elements with Visitors

Figure 3: ObjectStructure and IElement::accept() double-dispatch link

Figure 3: ObjectStructure and IElement::accept() double-dispatch link

One of the key participants in the Visitor pattern is the ObjectStructure class. The Gang of Four introduce the ObjectStructure participant to handle traversing the Element objects that may need the attention of a Visitor. The pattern solves the double-dispatch problem by including a concrete visitor in the Element::accept(IVisitor) method. However, the path from Client-request to double-dispatch first goes through the ObjectStructure class. By looking the Client, ObjectStructure and a concrete Element, you can see the double-dispatch process:

< ?php
/*
* CLIENT
*/
//Client.php
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
//Autoload code
function includeAll($className)
{
    include_once($className . '.php');
}
spl_autoload_register('includeAll');
 
//Begin Client class
class Client
{
    private static $shapeElement;
    private static $color;
    //client request
    public static function request()
    {
      self::$shapeElement=$_POST['shape'];
      self::$color=$_POST['color'];
 
      $obStructure = new ObjectStructure();
      $obStructure->attach(new self::$shapeElement());
      $colorVisitor= new self::$color();
      echo $obStructure->confirm($colorVisitor);
    } 
}
Client::request();
?>
 
 
< ?php
/*
* OBJECT STRUCTURE
*/
//ObjectStructure.php
class ObjectStructure
{
    private $elements=array();
 
    public function attach(IElement $element)
    {
         array_push($this->elements,$element);
    }
 
    public function confirm(IVisitor $visitor)
    {
         foreach($this->elements as $elementNow)
         {
              return $elementNow->accept($visitor);
         }
    }
}
?>
 
 
< ?php
/*
* ELEMENT
*/
//Circle.php
class Circle implements IElement
{
    private $visColor;
 
    public function accept(IVisitor $visitor)
    {
        $visitor->visitCircle($this);
        return $this->showShape();
    }
 
    private function showShape()
    {
        $circleShape= IElement::SVG . "<circle cx='110' cy='110' r='100' fill='{$this->doColor()}' stroke='blue' stroke-width='1' />";
        return $circleShape;
    }
 
    public function setColor($visitorColor)
    {
         $this->visColor = $visitorColor;
    }
 
    private function doColor()
    {
        return $this-> visColor;
    }
}
?>
 
</circle>

Stepping through the process, the Client first (1) instantiates an instance of ObjectStructure. Second (2) the Client uses the ObjectStructure::attach() method to push the selected element instance onto an array. Third (3) the Client passes the selected visitor to the ObjectStructure::confirm() method, which in turn, fourth (4) calls the Element::accept($v) method which passes the concrete visitor to all of the elements in the array. Since this example is very simple, it only includes one element and one visitor at a time, and so the array will only contain a single element. However, because of the ObjectStructure class, you can add more elements if needed. (The following sections show more detail on how double-dispatch works and how the visitors are implemented.)
Continue reading ‘PHP Visitor Design Pattern II: The Double Dispatch’

Share

PHP Visitor Design Pattern I: The Single Dispatch

visitorTime to Add an Operation

I’ve always liked the Visitor Pattern, but it can appear somewhat daunting from the looks of the pattern’s class diagram. However, by easing into it, it’s fairly manageable and quite useful. In a nutshell, the Visitor allows developers to create programs that perform operations on elements in an object structure without changing the classes subject to the operation. In some respects this sounds a lot like the Decorator pattern but instead of adding properties, the Visitor pattern “visits” the structure with required operations.

Where the visitor comes into play is when you have a set of objects that share a common interface, but some–just some–need a method that does something not part of the interface, but it should not disrupt the interface or the related objects that do not need the method’s operation. In situations where added requirements crop up for extant structures, the Visitor is a welcomed guest.

An Element and a Pretend Visitor

To get started, instead of looking at the class diagram for the whole pattern, I want to take the Element interface and two concrete implementations of that interface as a point of departure. This particular set of concrete Element implementations create shapes using SVG graphics. One implementation creates a circle and the other a a square. Click the Play button to see what the program does and the Download button to view the files:
PlayDownload

A visitor object is one that adds an operation to an existing object without changing the object in the context of its interface. This first implementation assumes that the developer just wanted to make shapes and did not want to add fill color; so the fill color attribute of the SVG element has been left blank. (If no value is entered in the color attribute, it defaults to black–more on that later.)

In order to to show how a blank color is filled, the two shape-making implementations (Circle and Square) have a “pretend visitor.” The “visitor” is nothing but a private method that adds color. It is instructive insofar as it illustrates how to create an operation to add color to an existing method within a class.

First, take a look at the Element interface (IElement). It contains a constant with an immutable state and two methods; one for returning an object and the other the “pretend” visitor” supplies color to an otherwise colorless shape.

 
interface IElement
{
    //Constant for mutually shared code
    const SVG ="<svg width='2.2cm' height='2.2cm' viewBox='0 0 220 220'>";
    //Return object
    function showShape();
 
    //Pretend visitor
    function doColor();
}
?>
</svg>

Next, two implementations of the IElement create Square and Circle classes. Importing the SVG element from the IElement interface (stored as a constant), each class simply returns the code for the requested shape.

//Square.php
< ?php
class Square implements IElement
{
    public function showShape()
    {
        $squareShape= IElement::SVG . "<rect x='1' y='1' width='200' height='200' fill='{$this->doColor()}' stroke='blue' stroke-width='1' />";
        return $squareShape;
    }
 
    //Pretend visitor
    function doColor()
    {
        //pretent visitor red
        return "#b00";
    }
}
?>
 
//Circle.php
< ?php
class Circle implements IElement
{
    public function showShape()
    {
        $circleShape= IElement::SVG . "<circle cx='110' cy='110' r='100' fill='{$this->doColor()}' stroke='blue' stroke-width='1' />";
        return $circleShape;
    }
 
    //Pretend visitor
    function doColor()
    {
        //pretent visitor green
        return "#0b0";
    }
}
?>

The pretend visitor is the doColor() method. It acts like a coloring operation that is coming from “somewhere else.” Subsequent posts examine how a real visitor works, but for now, just take a look at how an outside operation is used to establish color in the showShape() method. (Continue to learn about the roles of the Client and Single-Dispatch.) Continue reading ‘PHP Visitor Design Pattern I: The Single Dispatch’

Share

PHP Functional Programming Part III: λ Lambda Calculus

lambdaCalcλ After learning how to program in Haskell, a pure functional programming language, in the edX course offered through TU DelftX I’m convinced more than ever that to do decent functional programming, you need to understand lambda calculus. I’m not saying that you need to master lambda calculus, but you need to understand it insofar as it applies to functional programming and differentiates imperative programming (what we do in sequential, procedural and OOP with PHP, Java, JavaScript, C++, and C#) and functional programming based on lambda calculus. In this post, I’d like to ease into lambda calculus and illustrate how it applies to functional programming in PHP.

It’s Not Like Other Programming Languages

A typical value a programmer may want to generate for a business site is the cost of an item plus shipping charges. By way of example, suppose that the shipping charges are all 11% of the cost of the item. You might write something like the following:

$priceNship = 14.95 + (14.95 * .11);

That’s not especially useful since you need to have a separate set of literals for each item to enter into the variable $priceNship.

You’re most likely to set up a method that handles such calculations with a variable generated through an argument. For example, you might have a class that looks like the following class and method:

<?php
class RetailStore
{ 
    private $priceNship;
 
    public function addShipping($x)
    {
        $this->priceNship = $x + ($x * .11);
        return round($this->priceNship,2);
    }
}
$worker = new RetailStore();
echo "Cost plus shipping: $" .  $worker->addShipping(14.95);
?>

I used the $x identifier for the parameter for the addShipping() method instead of something more descriptive like $cost because you’ll often see an x-named variable in lambda calculus.

Casting in Lambda Calculus

When using lambda calculus, one of the key characteristics that I had problems getting used to in Haskell was what you might call stating a problem or abstracting a problem. The problem statement is an abstraction of the problem you want to solve using functional programming. Because lambda calculus strives for abstraction, we’ll start with a simple one:

λx.x + (.11 (x))

What does that mean? First of all λx denotes a lambda function. The x variable is bound to the λ function. (It’s known as a bound variable as opposed to free variables. This post deals with bound variables only.) Put into a PHP class and methods, and setting up a return routine, you have the following:

?php
class RetailStoreFunc
{    
    public function addShipping($x)
    {
        // λx.x + (.11 (x))
        $priceNship = function ($x) {return $x + ($x * .11);};
 
        //Round off to 2 decimal points and return
        return round($priceNship($x),2);
    }
}
$worker = new RetailStoreFunc();
echo "Cost plus shipping: $" . $worker->addShipping(14.95);
?>

The λ function (lambda function) shows that the x value is added to the result of .11 times the value of x. In PHP, a λ function is the same as an anonymous or unnamed function. Now, looking at the PHP version of the λ function, you can see:

λx.x + (.11 (x)) = function ($x) {$x + (.11 *$x);};

If you compare the λ calculus with the PHP function, you can see that while the PHP function is a bit less abstract than the λ calculus expression, they are almost identical. Think of the λ calculus as nothing more than an abstraction of a solution.

If you can understand these introductory elements of λ calculus as applied to PHP, not only are you on the path to understanding λ calculus but also PHP functional programming. It’s not as difficult or large as other calculi; so, rest easy and continue.

Functions within Functions

Not only can λ functions return calculated values, they can return other λ functions. So, if you build one λ function, you can use it in another λ function. In this way you can build functional programs. Further, I have not found any contradiction between λ functions and functional programming and OOP in PHP. This next example shows another example of a λ functions in an OOP context, but unlike the first one this one 1) incorporate and returns one λ function in another, and 2) uses a private variable. You cannot assign a λ function to a variable that is not local (part of the method), but you can assign a non-local variable (e.g., private, public or protected) to store the results of a λ function—just not the λ function itself.

<?php
class RetailStoreReady
{    
    private $final;
    public function addShipNtax($x)
    {
        // λx.x + (.11 (x))
        $priceNship = function ($x) {return $x + (.11 * $x);};
 
        // λx.λx + (.07 (x)) (λx. $priceNship(x) + .07 (x))
        $totalWithTax = function ($x) use ($priceNship) {return $priceNship($x) + (.07 * $x);};
 
        //Using an embedded property ($final) for storing results
        $this->final=round($totalWithTax($x),2);
        return $this->final;
    }
}
$worker = new RetailStoreReady();
echo "Cost plus shipping and tax: $" . $worker->addShipNtax(14.95);
?>

In PHP, employ the use statement when one or more λ functions are within a λ function. To employ more than a single λ function within another λ function, you can separate the λ functions by a comma in the use statement as the following shows:

function ($x) use ($lambdaA, $lambdaB) {return $lambdaA($x) + $lambdaB($x);};

The ability of PHP to incorporate these functional programming elements indicates the commitment the PHP community has to functional programming.

Do You Really Need λ Calculus to learn Functional Programming in PHP?

If you read Simon Holywell’s book Functional Programming in PHP, you will find mention of λ calculus, but only a mention. Indirectly, there are some examples, and I found that some basic understanding of λ calculus is very helpful. But λ calculus is not a requirement for functional programming in PHP or any other language. However, if you do understand something about λ calculus, it is extremely helpful to better grasp functional programming in virtually any programming language. In future posts on functional programming, I will be introducing more elements of λ calculus that pertain to better understanding and using functional programming in PHP.

Share