Skip to content
Archive of posts tagged OOP

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 [...]

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 [...]

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 [...]