Development suggestions

I offer the following descriptive image to Rasmus Lerdorf, Anders Hejlsberg, Brendan Eich and all other people who might be maintaining, developing or creating new languages. Programming can be cold, logical and too intuitive (varies massively between languages). I offer a possible solution to this dilemma:
Language improvements

I hope some of you will implement these features in the future. Leave a comment if you do!

PHP: Basic object oriented PHP

I’m writing this article to my beloved wife who is currently struggling to understand the OOP-part of PHP programming. If you come over from Java or C# to PHP then OOP is not a problem. If you come from [INSERT NON-PROGRAMMER-JOB HERE] then OOP is a rather complex issue. To understand it you need to understand what a class is and what it represents. To do that, you need to see what benefits a class provides over a more conventional approach.

What I thought I’d do is that I’ll go through what the actual parts of a class are and what they do.
I have created a class called Person (because I am mr Original Programmer) and it looks like this:

	/**
	 *	A class representing a person. This version of a person
	 *	has but two properties that defines it; name and mood.
	 */
	class Person
	{
		public $name; 	//This property can be accessed outside the class
		private $mood;	//This property can only be accessed from within the class
		
		/**
		 *	Set the mood to one of the following:
		 *	1: Happy
		 *	2: Angry
		 *	3: Sad
		 */
		public function setMood($val)
		{
			//Check if val is one of the acceptable moods
			if($val == 1 || $val == 2 || $val == 3)
			{
				$this->mood = $val; //Allright, $val is accepted, set mood to val
			}
			else
			{
				$this->mood = -1; //-1 means something is wrong
			}
		}
		
		/**
		 *	Returns the mood of the person
		 */
		public function getMood()
		{
			switch($this->mood)
			{
				case 1:
					return "Happy";
				case 2:
					return "Angry";
				case 3:
					return "Sad";
				case -1:
				default:
					return "Not specified";
			}
		}
	}

In the code above we have created a class called Person which will represent a Person. Think of it as a blueprint, or that picture of the human body that a doctor has on his/her wall. It’s not actually a person, it’s just the information that describes it. There are a lot of different reasons for programmers to create classes and in this case we need a class that represents data. Why? Well, I’ll show you an example in two parts. First we’ll collect and output some data about people without the use of a class:

Non-OOP

	//If we don't have classes, this is how we will have to 
	//handle working with data about people:
	$person1Name = "Joe";
	$person1Mood = "Happy";
	
	$person2Name = "Andy";
	$person2Mood = "angry";
	
	$person3Name = "Emilia";
	
	echo "The mood of $person1Name is $person1Mood<br />";
	echo "The mood of $person2Name is $person2Mood<br />";
	echo "The mood of $person3Name is $person3Mood<br />";
	
	//The above will output:
	//	The mood of Joe is Happy 
	//	The mood of Andy is angry 
	//	The mood of Emilia is 
	
	//If we want to loop this out we will have to do like this:
	$people = array(
		"Person1" => array(
			"name" => $person1Name,
			"mood" => $person1Mood
		),
		"Person2" => array(
			"name" => $person2Name,
			"mood" => $person2Mood
		),
		"Person3" => array(
			"name" => $person2Name,
			"mood" => null
		)
	);
	
	foreach($people as $person)
	{
		echo "The mood of {$person['name']} is {$person['mood']}<br />";
	}
	
	//The above will output:
	//	The mood of Joe is Happy 
	//	The mood of Andy is angry 
	//	The mood of Emilia is 

Now, instead of creating a lot of variables that are only connected by naming convention we can create instances of our Person class. An instance is a manifestation of the class we created earlier. It’s like the actual building of a house as compared to the drawing of the blueprint. There is only a single blue print, but there can be endless amounts of manifestations of that blueprint, and they don’t have to have the same property values (houses can have different colors and owners even if the blueprints are the same).

OOP

	$person1 = new Person();
	$person1->name = "Joe";
	$person1->setMood(1);
	
	$person2 = new Person();
	$person2->name = "Andy";
	$person2->setMood(2);
	
	$person3 = new Person();
	$person3->name = "Emilia";
	
	echo "The mood of {$person1->name} is {$person1->getMood()} <br />";
	echo "The mood of {$person2->name} is {$person2->getMood()} <br />";
	
	//Since we didn't set any mood for person3 she will default
	//when we call the getMood method.
	echo "The mood of {$person3->name} is {$person3->getMood()} <br />";
	
	//The above will output:
	//	The mood of Joe is Happy 
	//	The mood of Andy is Angry 
	//	The mood of Emilia is Not specified 
	
	
	
	//Another benefit of using classes instead of loads of 
	//independent variables is if you want to place all this
	//data into an array and loop it out:
	$people = array($person1, $person2, $person3);
	foreach($people as $person)
	{
		echo "The mood of {$person->name} is {$person->getMood()} <br />";
	}
	
	//The above will output:
	//	The mood of Joe is Happy 
	//	The mood of Andy is Angry 
	//	The mood of Emilia is Not specified 

So, what have we gained?

  • Improved readability
  • Less variables floating around
  • Easier to work with as a collection
  • Validation

To begin with readability. It matters. Unless you will outlive the language there a chance that someone else might have to go through some of your code one day or you might have to revisit some old code you wrote a long time ago which you don’t remember anymore. Anything that can improve the readability of you code is good and the objects now referred to as $person1, $person2 and $person3 are easy to understand and identify; they contain data about a Person.

The reduced amounts of variables are not just aesthetically pleasing, it reduces the chance of mistakes and makes it easier to work with collections. A single object containing information can easily be placed within an array whereas without it we need to give the data keys and place it inside nested arrays to keep a track of it. This simplifies both the creating of the collection and the subsequent processing of that collection.

Lastly we actually gain a chance to validate our data. In our initial, non-OOP, approach we just create variables for the moods and enter anything. As we create the class we disallow the direct usage of the property mood and instead create a getter and setter for the property. We turn it private, meaning it cannot be accessed from outside the class (i.e. on an object), and then create help methods to both set its value and retrieve it. This way we guarantee that the property mood cannot be set without going through the setMood method.
When we set the value we also validate it. If the value doesn’t pass validation we skip it and set the mood to a default value. Then when we use the get method we can make sure that only valid data is returned (in a conformed way).

That’s all great, but what about that horrid $this variable inside the class!?
Yea, about that. Well, as I said earlier the class works as a blueprint. The blueprint is the same for all people but everyone of our instances differ a little bit (both name and mood). Let’s look at the following part:

		private $mood;
		
		public function setMood($val)
		{
			if($val == 1 || $val == 2 || $val == 3)
			{
				$this->mood = $val;
			}
			else
			{
				$this->mood = -1; //-1 means something is wrong
			}
		}

Since we set mood to private and we set the value in the method setMood we end up in a situation where we don’t actually know when the code inside that method will execute. This is of course true for any method/function but the problem is that this method has access to class specific data, namely mood. Mood can be set on an object, and more importantly it can be set to different values on different objects. Once we’ve created an instance of the Person class the variable mood will be unique to that instance.
What $this does is that it represent that specific instance that you created. That makes it possible to work with the data inside the individual object while you design the class. You accept that you don’t know what object you are in during the execution of your method, but you want to set the value of mood in that particular object, not in any other. So $this represents that object:

	$person1 = new Person();
	$person1->name = "Joe";
	$person1->setMood(1);	//$this now represents $person1
	
	$person2 = new Person();
	$person2->name = "Andy";
	$person2->setMood(2);	//$this now represents $person2

Is that it? Yes! No no no, it isn’t, but hopefully this will give you a bit of a start. If you have any questions either leave them as comments or, if you are married to me, come and ask me in my den.

Posted in PHP

WAMP: Standard installation issues

I just installed a WAMP environment (Windows Apache Mysql PHP) on my computer as I haven’t really been writing any PHP-code recently and I felt like doing it again. I had forgotten the initial issues one might have when installing WAMP and so I though I’d just write down two typical problems you might have when installing your WAMP environment:

Forbidden You don’t have permission to access /phpmyadmin/ on this server.

This problem shows up when your phpmyadmin settings are set to deny all connections. Go to C:\wamp\alias and open phpmyadmin.conf and you’ll see the following section:

<Directory "c:/wamp/apps/phpmyadmin3.5.1/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>

change it to:

<Directory "c:/wamp/apps/phpmyadmin3.5.1/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Allow,Deny
    Allow from all
</Directory>

Restart WAMP.

You might also get the following problem:

Forbidden You don’t have permission to access / on this server.

If you get this problem you might’ve tried to access ‘localhost’ in your web browser. Try to access ‘127.0.0.1’ instead. If you can access 127.0.0.1 but not localhost you need to edit your httpd.conf in your Appache installtion.
Open the httpd.conf and find the following section:


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

change it to:


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 0.0.0.0:80

Restart WAMP.

Windows 8: Updating a live tile from a PHP service

As I am making my first Windows 8 application I want to add a live tile. The application is a fixture list for my football team, Malmö FF, and I want it to have a live tile that always shows the next game.
I have all the games listed on a site that I’ve written in PHP which the full application uses to get the fixture list so I thought it would be neat if it could also generate the live tile.

I read up on how to create live tiles using the following two resources:
http://msdn.microsoft.com/en-us/library/windows/apps/hh761476.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh761491.aspx

It is possible to direct the update function to a URL which contains the XML for the live tiles. I’ve called the file Tile.php and it looks like this:

<?php
	$myLongHeader = "Hello dear world!";
	$myLongInfo1 = "I am a happy camper";
	$myLongInfo2 = date("y-m-d");
	$myLongInfo3 = ":)";
	
	$myShortHeader = "Hello!";
	$myShortInfo1 = "I am happy!";
	$myShortInfo2 = date("ymd");
?>
<tile>
  <visual>
    <binding template="TileWideText01">
      <text id="1"><?php echo $myLongHeader; ?></text>
      <text id="2"><?php echo $myLongInfo1; ?></text>
      <text id="3"><?php echo $myLongInfo2; ?></text>
	  <text id="4"><?php echo $myLongInfo3; ?></text>
    </binding>  
    <binding template="TileSquareText01">
      <text id="1"><?php echo $myShortHeader; ?></text>
      <text id="2"><?php echo $myShortInfo1; ?></text>
      <text id="3"><?php echo $myShortInfo2; ?></text>
    </binding> 
  </visual>
</tile>

This then generates the following XML (with the correct date of course):

<tile>
  <visual>
    <binding template="TileWideText01">
      <text id="1">Hello dear world!</text>
      <text id="2">I am a happy camper</text>
      <text id="3">13-04-07</text>
	  <text id="4">:)</text>
    </binding>  
    <binding template="TileSquareText01">
      <text id="1">Hello!</text>
      <text id="2">I am happy!</text>
      <text id="3">130407</text>
    </binding> 
  </visual>
</tile>

Notice that you can add both a large and a small tile. This is useful it seems that there will be support for even more sizes in the coming future according to Microsoft.
To use this from your application do the following in your C# code. You only have to do this once during the installation process of your application:

public class TileManager
{
	private const string Url = "http://mysite.com/services/Tile.php";

	public static void CreateUrlUpdater()
	{
		var recurrence = Windows.UI.Notifications.PeriodicUpdateRecurrence.HalfHour;
		var url = new Uri(Url);

		Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().StartPeriodicUpdate(url, recurrence);
	}
}

PHP: Enable CURL in WAMP environment

So I needed to enable Curl to run a PHP script today and my WAMP installation didn’t have CURL enabled “out-of-the-box”. This was however easily fixed by following these steps:

  1. Close WAMP
  2. Go to your WAMP installation directory and enter bin\php\php[VERSION] (normally this will be C:\wamp\bin\php\php[VERSION])
  3. Open php.ini in your favorite editor
  4. Search for curl and you’ll find a line similar to “;extension=php_curl.dll“, uncomment it (remove the semi-colon)
  5. Save and close the file
  6. Go to your WAMP installation directory and enter bin\apache\apache[VERSION]\bin\ (normally this will be C:\wamp\bin\apache\apache[VERSION]\bin)
  7. Open php.ini in your favorite editor
  8. Search for curl and you’ll find a line similar to “;extension=php_curl.dll“, uncomment it (remove the semi-colon)
  9. Save and close the file
  10. Restart WAMP

 

PHP: Using preg_replace_callback in an OOP environment

As I was using the preg_replace_callback function today I realized I had no idea how to use it in an OOP environment. I deviced the following example to show how to use the function inside a class where the callback is in fact a function in the class:

<?php 
 
class TestClass
{
	private function testCallback($text)
	{
		$age = $text[0];
		$age++;
		return $age;
	}
 
	public function parse($text)
	{
		return preg_replace_callback(
			"([0-9]+)", 
			array($this, 'testCallback'), 
			$text);
	}
}
 
$testClass = new TestClass();
$text = "My name is XXX and I'm 78 years old!";
 
$newText = $testClass->parse($text);
 
echo "Old text: $text<br />";
echo "New text: $newText<br />";
 
?>

This would output:

Old text: My name is XXX and I'm 78 years old!
New text: My name is XXX and I'm 79 years old!
Posted in PHP