Archive for the ‘Tutorial’ Category

No require with autoload

2. January 2011

No Comments »

If you have ever worked with a PHP framework you probably have noticed some features that allow you to develop code more rapidly. In this blog, I am going to address autoloading.

Traditionally, in object oriented PHP, we create a new source file for each class. If we want to create an object of a class, we have to include the php file using require_once (or using a similar function). If we do not include the source file, we will be issued a fatal error.

Fatal error: Class 'User' not found in ...

Lets say we have a User class defined as follows in a User.php file

<?php

class User {
    function __construct() {
        echo "Hello, World!";
    }
}
?>

If we want to create a new User object we would include user.php and use the new keyword to instantiate the class.

<?php
require_once('user.php');
$user = new User();

This is method reasonable for a small number of classes. However, large frameworks can span hundreds of classes, and having to write require_once for each class becomes a huge annoyance. PHP5 introduced an autoload magic method which is automatically called if the class source file cannot be found. The __autoload function takes a single parameter – the class being instantiated. If our class name corresponds to its file name we can do something as simple as:

<?php
function __autoload($class) {
    require_once($class . ".php");
}

Now, each time you instantiate a class, the autoload function will be automatically ran and it will automagically require your class. This is great, however all classes have to be in the same directory as the callee, and any decent framework uses a class hierarchy and spreads classes out into different directories. When I was writing my framework a few years ago, I stumbled upon a pretty awesome use of the autoload function. I think the idea was borrowed from the Zend framework. In a nutshell, the class names would correspond to their location on the file system. For example, the Zend framework has a class named Zend_Controller_Action_HelperBroker, and the class path Zend/Controller/Action/HelperBroker.php. What does this mean? With some additional code in our autoload function, we can convert the class name into its respective path allowing use to create a class hierarchy on our file system.

<?php
function __autoload($class) {
    $file = str_replace("_", "/", $class);
    require_once($file . ".php");
}

Now, with the onset of PHP5.3 and the introduction of namespaces, this method has been kind of depreciated for a more pragmatic method. Instead of naming our classes with really long names that correspond to their path, we can use a namespace instead. Thus our autoload function would look like:

<?php
function __autoload($class) {
    $file = str_replace("\\", "/", $class);
    require_once($file . ".php");
}

In my next blog, I will talk more about namespaces and give a better demo on how this is used.

Sending Text Messages in PHP

7. August 2010

3 Comments »

Recently I’ve had to implement an alert system in PHP were the alerts are sent via a text message. The concept is very simple – use an email to SMS gateway. Essentially, you send an email to a phone companies gateway server, that server then relays the email to the phone number specified via a SMS. Since PHP has the ability to send emails, the whole process is quite easy.

1. First you need to acquire a list of SMS gateways you are going to support. For an extensive list, check out Wikipedias List of SMS Gateways. For this tutorial, I will only be using AT&T Wireless (number@txt.att.net) and Verison (number@vtext.com).

2. Create a simple HTML form.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"></meta>
	<title>SMS</title>
</head>

<body>
	<form action="POST">

		<label for="provider">Provider: </label>
		<select>
			<option value="txt.att.net">AT&T Wireless</option>
			<option value="vtext.com">Verizon</option>
		</select><br />

		<label for="number">Number: </label>
		<input type="number" /><br />

		<label for="subject">Subject: </label>
		<input type="subject" /><br />

		<label for="message">Message: </label>
		<input type="message" /><br />

		<input type="submit" />

	</form>

</body>

</html>

3. Add the necessary PHP code to send the email

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"></meta>
	<title>SMS</title>
</head>

<body>
	<?php
		if(!empty($_POST['number'])) {
			mail($_POST['number'] . $_POST['provider'], $_POST['subject'], $_POST['message']);
        }
	?>

	<form action="POST">

		<label for="provider">Provider: </label>
		<select>
			<option value="@txt.att.net">AT&T Wireless</option>
			<option value="@vtext.com">Verizon</option>
		</select><br />

		<label for="number">Number: </label>
		<input type="number" /><br />

		<label for="subject">Subject: </label>
		<input type="subject" /><br />

		<label for="message">Message: </label>
		<input type="message" /><br />

		<input type="submit" />

	</form>

</body>

</html>

You will want to do some better error checking and validation, but thats it!

HTML5 Input

1. August 2010

2 Comments »

Lets be honest, HTML5 is the new buzz word. Every blog, tutorial, tweet, article, or post I read contains some reference to HTML5. It’s almost as bad as presidential campaigns during election year (well… maybe not that bad, but you get the idea!) However, unlike the latter, I am fully confident HTML5 will live up its expectations. Why is HTML5 so special? Well, you know all those hoops you’ve had to jump through and JavaScript libraries you’ve had to include? Kiss them goodbye! Let us take a look at some of the new features HTML5 provides with regard to input types.

Placeholder

<input name="first" placeholder="First Name" />

Range

<input name="range" type="range" value="50" min="0" max="100" />

Number

<input type="number" min="0" max="12" step="3" value="6" /> 

Search

<input type="search" /> 

Date

<input type="date" /> 

Month

<input type="month" /> 

Week

<input type="week" /> 

Time

<input type="time" /> 

Datetime

<input type="datetime" /> 

Datetime-Local

<input type="datetime-local" /> 

Currently, desktop browsers treat the following types as plain text. However, Safari on my iPhone optimizes the keyboard layout.

Email

<input type="email" />

URL

<input type="url" />

Tel

<input type="tel" />

The next type is built into the HTML5 specification, but I could not find any browser that takes advantage of it.

Color

<input type="color" />