Object Iteration in PHP 5

PHP 5 gives us the ability to iterate through objects much as we can with arrays, such as with the foreach() loop construct. I knew this ability existed but had not really looked into it or made use of it. However as a result of a thread at PHPBuilder.com, I thought this might be a good solution.

The key here is that while the default behavior of object iteration is to access each of the object’s public properties, you can override that behavior via the Iterator interface, defining custom methods to iterate what you specifically want from the object.

Continue reading “Object Iteration in PHP 5”

Beginners’ Corner: Learning Object-Oriented PHP

I often see PHP newbies (and even not-so-newbies) who are confused by the world of object-oriented programming (OOP). At least part of this confusion results from the vast majority of introductory books and tutorials for PHP beginning by teaching procedural programming techniques, treating OOP as an “advanced” subject with a chapter or two at the end. Thus the student is exposed to a bunch of new syntax concerning classes and objects with, at best, a very brief explanation as to “why” that syntax should be used. A purely object-oriented language such as Java or Ruby, on the other hand, will by its nature require that beginners’ resources more fully address the theory of OOP.

I am not going to claim to be an OOP guru, but I can point you to a few resources I have used to become reasonably proficient — but still learning! — with PHP classes and objects. First I would point you to an on-line tutorial at Sun.com: “Lesson: Object-Oriented Programming Concepts“. While the (relatively few) examples in these tutorials are in Java, the syntax is quite similar to that of PHP, and the lessons they illustrate are easily transferred to PHP. If you want to work with those examples yourself in PHP, I would recommend taking a look at a couple PHP tutorials at IBM.com: “Getting started with objects in PHP V5” and “Advanced PHP V5 objects“.

Once you’ve gotten past the basics, to see how things can work together as an OOP whole, I strongly suggest you get your hands on Matt Zandstra’s book, PHP Objects, Patterns, and Practice. Reading this book is what ultimately made objects “click” in my mind. After reading it I felt much more comfortable with the underlying concepts along with the specific techniques for applying them in my PHP projects.

Using Akismet to Detect Spam Email

After seeing the effectiveness of the Akisment WordPress plug-in at filtering out spam comments here, I decided to see if I could use it in conjunction with a email contact form. I thought it might be interesting to some of my readers (there are at least a couple) to keep a sort of journal here of what I do to accomplish that, plus it might help encourage me to finish it.

So the first thing I’ve done is to create an Akismet class that can take the pertinent data, contact the Akismet server via cURL and send it that data, and then return the response as a boolean (true == spam, false == not spam).

Continue reading “Using Akismet to Detect Spam Email”

Application Constants in Interfaces

Here’s a little trick I discovered the other day for passing application settings around in an object-oriented implementation. You can create an interface that defines any number of class constants, then any class you define that needs those constants needs only to implement that interface. For example:

<?php
/**
 * Define constants for use in other classes
 */
interface Constants
{
   const DB_HOST 'localhost';
   const DB_USER 'username';
   const DB_PASS 'abc123xyzr';
   const DB_NAME 'test';
}

/**
 * Database class based on MySQLi class
 */
class DB extends mysqli implements Constants
{   
   public function __construct()
   {
      parent::__construct(
         self::DB_HOST,
         self::DB_USER,
         self::DB_PASS,
         self::DB_NAME
      );
   }
}

The advantage of this over running some configuration script that sets the constants is that by implementing an interface, it becomes immediately visible that the class requires that interface. If on the other hand you depend on independently setting constants in an include file or such, then if you try to reuse a class that uses those constants, it will not be immediately obvious that they are needed until you start testing it in the new implementation.

The main limitation is that interfaces cannot have class variables, only constants. If you need application-wide variables for your object-oriented application, you’ll either need to instantiate a class that has those variables and pass it to each object that needs it, use a singleton pattern class (which can have the same disadvantage of globals in that the fact that it is required can be hidden inside a class), or look into something like using a registry pattern class.