Review of Curry Capital, Brick Lane, London

A rather good curry

I’ve tried the Brick Lane curry experience a few times now, as I live in
East London and work in Bethnal Green, but have always been disappointed. I
think out of the worst half a dozen curries I’ve ever had in my life, Brick
Lane has accounted for about five of them.

Curry Capital, then, was a real find. It’s slightly further up the north end
of Brick Lane than the large gaggle of Indian and Bangladeshi restaurants
usually are and is almost on its own. There’s no-one outside touting free
beer, wine or starters (a very good sign) and the decor is quite
sophisticated… relative to the rest of Brick Lane’s offerings, of
course.

The food is also pretty good – certainly well above anything else I’ve had
down London’s most famous curry street. The “Katmandu Delicacy” I chose had
very tasty fresh chillies, and lamb that didn’t require oversized muscles to
chew on. The sauce was richly flavoured and the side dishes were equally
good.

Although the menu did offer what they called ‘Old favourites’, the main
focus was definitely on the Chef’s Specialities listing. These dishes were
the most interesting and the descriptions of each makes them very
appetising.

Beer was the usual offering of Cobra or Kingfisher. Prices came to
£25/head for main course, a couple of beers, rice, naan and a couple of side
dishes, which seemed very reasonable given the quality

PHP 5: Class hinting

Class hinting is where the class of object to be passed into a method is specified in the function call.

e.g.

abstract class User
{
 protected $logState;

 public function User()
 {
  $this->logState = new LogState()
 }

 public function setLogstate(LogState $logState)
 {
  $this->logState = $logState;
 }
}

This code will throw a runtime error if the variable passed into the setLogstate method is anything but a LogState object.

The benefits of this are twofold:

  • Errors in code are trapped at an earlier point in runtime. If the type was not checked then $this->logState may contain, for example, a string or other variable type and the mistake not caught until much further through processing making it more difficult to locate the source of the error.
  • The code is self-documenting as it is obvious to anyone reading the setLogstate method what class of object $logState will contain

The hint may not just be a class, it can also contain the name of an interface. Hence a method such as:

function saveSomething(Saveable $objectToSave)
{
 //Do things with $objectToSave
} 

will allow through any object which implements the Saveable interface.

PHP 5: Interfaces

Interfaces are a way of adding the definition of extra methods onto a class to force it to conform to a particular pattern. An interface consists entirely of methods with empty bodies i.e. abstract methods:

interface Saveable()
{
 public function save();
}

Classes implement interfaces as a way of guaranteeing that they will allow a set of operations as specified by the interface. E.g.

abstract class User implements Saveable
{
 /*
  Previous code for User here
 */
 public function save()
 {
  //Code in here for saving to disk
 }
} 

Interfaces are useful primarily to help document and constrain code, especially when used with class hinting (see next section). A class may implement more than one interface, separated by a comma e.g.

class User implements Saveable, Cacheable 

Advanced uses of Interfaces allows the treatment of objects as arrays (by implementing Iterator, ArrayAggregator and a number of Standard PHP Library interfaces).

Next section: Class hinting

PHP 5: Static classes

Static classes are used for utility scripts that (in the past) would have been in common functions or held global variables. The advantage of using a static class is more to increase code readability (and ease of debugging) than for any other reason. A static class may have properties and methods but there will always be only one ‘instance’ (hence static) and any changes will be made ‘globally’.

Static classes are often used for algorithms that may be used by multiple classes. They may also be used to construct objects of a specific type and to abstract logic from classes (to encourage loose coupling of objects – i.e. the ability of one class to operate without dependency on another). The example that follows allows the passing in of a username and password and will decide which class of user to instantiate and return. This is important in the case where we want to create a User object, but we have now defined the User class as abstract – how do we know what class of User to create?

class UserHandling
{
 public static $numUsers = 0;

 //a static class has no constructor

 /*
  This static method accepts a username and password
  and returns an object of type User
 */
 static public function makeUser($username,$password)
 {
  /*
   Logic in here to look in a database
   and get a userTypeID based on username and
   password
  */
  if($isValidUser)
  {
   if($isAdmin)
   {
    $user = new AdminUser();
   }
  else
  {
    $user = new RegisteredUser();
  }
  }
  else
  {
   $user = new UnregisteredUser();
  }
  UserHandling::$numUsers++;
  //Changing a static variable
  //inside the class still
  //needs the full reference.
  //There is no “$this” inside a
  //static class.
  return $user;
 }
}

//To make a user object, from anywhere in the code
$user = UserHandling::makeUser($username,$password); 

Properties accessed from outside a static class (if they’re public) are also handled in the same way:

echo UserHandling::$numUsers;

Next section: Interfaces

PHP 5: Abstract classes

Abstract classes are used to define operations and parameters but where you do not want a class instantiated directly. An abstract class must, therefore, have subclasses. In our example, we may not allow a type of general “User” but insist that a specific type is created. We can do this by defining the class as abstract:

abstract class User
{
 protected $logState;

 function User()
 {
  $this->logState = new LogState()
 }

 public function setLogin($loginValue)
 {
  $this->logState->setLogin(true);
 }

}

A call to $user = new User(); will now be invalid. Instead, we must call a subclass e.g. AdminUser.

An abstract class may contain abstract methods. These define the template for the method that must be implemented by each subclass. Abstract methods have no body, as with showNavigation() below:

abstract class User
{
 protected $logState;

 public function User()
 {
  $this->logState = new LogState()
 }

 public function setLogin($loginValue)
 {
  $this->logState->setLogin(true);
 }

 abstract public function showNavigation();
}

It would then be compulsory for each and every subclass of User to define a method called showNavigation() and to provide body code to perform the operation.

Next section: Static classes

PHP 5: Public, private, protected

PHP 5 allows you to declare properties and methods as public, private or protected. These are defined as:

  • Public: anyone either inside the class or outside can access them
  • Private: only the specified class can access them. Even subclasses will be denied access.
  • Protected: only the specified class and subclasses can access them

It is good practice to mark properties as protected (at least) and to use get/set methods to change their value. This future-proofs the code and can reduce the number of changes that need to be made. E.g. the existing user code makes use of an isLoggedIn variable. Making the variable protected and adding a method to set its value like this:

class User
{
 protected $isLoggedIn = false;

 function User()
 {
 }

 function setLogin($loginValue)
 {
  $this->isLoggedIn = $loginValue;
 }

}

will prevent code outside of the class (or subclasses) from changing the value of isLoggedIn without going through the setLogin() method. Although it may seem long-winded, the code of User may change in the future e.g. with the addition of a ‘LogState’ object to handle the logging in status:

class LogState
{
 protected $isLoggedIn = false;

 function __construct()
 {
 }

 function setLogin($isLoggedIn)
 {
  $this->isLoggedIn = $isLoggedIn;
 }
}

class User
{
 protected $logState;

 public function User()
 {
  $this->logState = new LogState()
 }

 public function setLogin($loginValue)
 {
  $this->logState->setLogin(true);
 }

}

If access to the logged in state of the user is protected then every change to it will be through the setLogin method. Now that a new object is controlling the state, only the method needs to change and no other code will notice the alteration.
Protected and private methods prevent outside calls to methods which may upset the internal state of an object.

Next section: Abstract classes

PHP 5: The constructor method

The introduction of a method called __construct() means that function User() no longer need be the default method name for instantiating an instance of the User class. This is especially useful in the case where you are using subclasses, or if the name of the class changes during development. E.g. In the example above, the constructor for User is called User() and the constructor for AdminUser is called AdminUser().

class User
{
 function __construct()
 {
 }
}
class AdminUser extends User
{
 //No constructor in here, so we’ll
 //use the parent’s code
}

Alternatively, the AdminUser constructor may use the majority of code from its parent but may also add to it. The parent’s constructor may then be called from within the child class.

class User
{
 function __construct()
 {
 }
}

class AdminUser extends User
{
 function __construct()
 {
  $isAdmin=true;
  //Call the parent’s constructor now
  parent::__construct();
 }
}

Next section: Public, private, protected

PHP 5: Subclasses

Subclassing allows flexibility in code. E.g. there may be many types of User to a site. We can define subclasses of User such as AdminUser, RegisteredUser, UnregisteredUser which may share some common methods and properties but may have unique abilities. The syntax for this is simple:

class User
{
 //constructor function for User
 function User()
 {
 }
}
class AdminUser extends User
{
 //constructor function for AdminUser
 function AdminUser()
 {
 }
}

The method of logging in for all users may be the same, so we add a method to the User class:

class User
{
 //constructor function for User
 function User()
 {
 }

 function doLogin($username,$password)
 {
  //Code in here
  return $result;
 }
} 

As AdminUser extends User the method doLogin() will be defined (identically) for both classes.

Alternatively, a new method called doLogin() could be written specifically for AdminUser to handle a distinct situation. This can be tackled on a subclass-by-subclass basis e.g. AdminUser and RegisteredUser may use the common doLogin() method but UnregisteredUser has its own version.

N.B. The signatures of methods in subclasses must match that of the parent class (as PHP does not support overloading). Therefore a definition of

Public function doLogin($someArray)

In AdminUser would be invalid code (as it only accepts a single variable) and caught at compilation time (i.e. before runtime).

Next section: The constructor method

PHP 5: By reference or by value

PHP 4 passed variables by value by default. This was not very pleasant to work with. The way around this was to pepper your code with ampersands (to tell PHP to use references instead) but forgetting to include one was difficult to debug. By default, PHP 5 passes all objects by reference. Therefore calling:

$user->getLogin($_SESSION);
print_r($_SESSION);
class User
{
 function getLogin($sessionArray)
 {
  //Code in here
  $sessionArray[] = “another value”;
 }
}

will result in exactly the same object (the session array) being passed into the method and used by it throughout. As we are adding an extra value to it the session array outside of the method will also change.

N.B. this isn’t particularly good practice in itself as it is not entirely clear from the line User.getLogin($_SESSION); that the $_SESSION array is being changed. A more self-documenting way or writing this would be $_SESSION = User.getLogin($_SESSION); and to return the $sessionArray variable from the getLogin() method.

Next section: Subclasses

PHP 5: Introduction to classes

Classes are used as an abstraction of a real world situation. E.g

class User
{
}

may be used to contain the code that will represent a user of an application.

Objects have properties and methods. Properties may be thought of as adjectives (e.g. coat->blue) and methods as verbs (e.g. coat->putOn()).

e.g. to create a user class, instantiate it and set a login property to true

class User
{
 var $isLoggedIn = false;
 //Variable to contain the logged-in
 //state, with a default value of ‘false’

 function User()
 {
 }

 function login($loginValue)
 {
  $this->isLoggedIn = true;
 }

}
$user = new User();
$user->login(true);

will create the user with a default property (isLoggedIn) and then log the user in with a method (login).

Next section: By reference or by value

Read more at:

PHP 5 Classes and objects on php.net