O'Reilly logo

Learning PHP Design Patterns by William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. A Universal Class for Connections and a Proxy Pattern for Security

It is art that makes life, makes interest, makes importance... and I know of no substitute whatever for the force and beauty of its process.

Henry James

Substitute “damn” every time you’re inclined to write “very”; your editor will delete it and the writing will be just as it should be.

Mark Twain

The work of science is to substitute facts for appearances, and demonstrations for impressions.

John Ruskin

A Simple Interface and Class for MySQL

In Chapter 2, you saw one example of how PHP allows interfaces to store constants that can be used by classes that implement the interface. One set of constants that can be stored in the interface is that required for connecting to a MySQL database. The routine using the PHP mysqli extension provides some variations for connection, but all variations need the host, username, password, and database information. Once the routine is set in a class, it should be available for general reuse whenever a program requires a MySQL connection. Figure 11-1 shows the class diagram for this arrangement.

Universal MySQL class diagram
Figure 11-1. Universal MySQL class diagram

The connection information is independent of the client because the concrete information is stored in constants in the interface. The UniversalConnect class uses the Scope Resolution Operator to access the data stored in the constants in the interface. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required