<?php

// Create a new namespace for the classes in this file (introduced in PHP 5.3)
// Access classes include the .php file and the "use" statement
// i.e. 
// require_once("Objects.php");
// use \Animals;
namespace Animals;			// Must be first statement in file

// Create a class with attributes
class Dog
{
	// Attributes
	// Access modifiers are public, private and protected
	// When using attributes, dont use the dollar
	// i.e. $dog->_name = "Bonzo";
	// WARNING:
	// Yuch... If you accidentally miss name an attribute, PHP
	// creates a new attribute for that particular object. This
	// is very broken behaviour, so be careful.
	//
	private $_name = "";		// Mikes naming convention, add underscore to name
	private $_age;
	private $_breed;
	
	// Constructor
	// You can use the name of the class i.e.
	// public function Dog($name, $age, $breed)
	// From PHP5 we should use __constructor, for inheritance,
	// this allows the name of the parent class to change.
	public function __construct($name, $age, $breed)
	{
		$this->_name = $name;		// Must use $this, to distinguish between local variables
		$this->_age = $age;			// in this function and the objects attributes.
		$this->_breed = $breed;
	}
		
	// Destructor also available
//	private function __destruct()
//	{
//		if(!empty($this->_name))
//		{
//			// If attribute exists, do something
//		}
//	}
	
	// Methods
	// Access modifiers are public, private and protected
	// By default PHP methods are public (Bad news!!)
	
	// To simulate get and set property accessors we use interceptor methods
	// We should move these into a parent class
	public function __get($property)
	{
		$method = "Get{$property}";			// Create method name e.g. "GetName", in local variable $method
		if(method_exists($this, $method))	// Check function exists
		{
			return $this->$method();		// Call it
		}
		return;
	}
	
	public function __set($property, $value)
	{
		$method = "Set{$property}";
		if(method_exists($this, $method))
		{
			$this->$method($value);
		}
		return;
	}
	
	// Methods for properties
	// Getters and Setters
	
	// We could make the Get/Set functions private,
	// but there is little to be gained since we
	// don't really mind if the user uses them
	// instead of using the "property" syntax.
	public function GetName()
	{
		return $this->_name; 
	}
	
	public function SetName($value)
	{
		$this->_name = $value;
		return;
	}	
}

/*
 * Exception handling provided
 * try
 * {
 * }
 * catch(Exception $e)
 * {
 * 		die($e->__toString(););
 * }
 * 
 * Can subclass the Exception class
 * class MyException extends Exception
 * {
 * }
 */

?>