• Date: June 25, 2011
  • Author: Slawek Lukasiewicz
  • Comments: 2 Comments
  • Category: PHP

Method chaining

Method chaining technique provides possibility to invoke method on returned object.

$someObject->doSomethingAndReturnObject()->invokeMethodOnReturnedObject();

Method chaining is a technique familiar to everyone using PHP libraries like Doctrine. It can make code cleaner and more readable.

class Calculator
{
 
	private $_amount = 0;
 
	public function add($amount)
	{
		$this->_amount += $amount;
 
		return $this;
	}
 
	public function multiply($amount)
	{
		$this->_amount *= $amount;
 
		return $this;
	}
 
	public function getAmount()
	{
 
		return $this->_amount;
	}
 
	public function __toString()
	{
 
		return (string)$this->getAmount();
	}
}
 
$calculatorInstance = new Calculator();
$calculatorInstance->add(5)->multiply(10);
print $calculatorInstance;
# 50

This solution is also known as Fluent interface pattern.

In example above methods return current object, but they can return different object instead.

$instanceOfClassA->doSomethingAndReturnInstanceOfClassB()->invokeMethodOnInstanceOfClassB();
  • Date: June 12, 2011
  • Author: Slawek Lukasiewicz
  • Comments: 3 Comments
  • Category: PHP

Create alias for class

Sometimes we need to use external libraries with strange and obscure class names, and it can be really annoying. Luckily, from PHP 5.3 version we can create aliases for classes and interfaces with class_alias function.

Example of class_alias usage.

class Irrational_Long_Class_Name
{
 // empty class
}
 
class_alias('Irrational_Long_Class_Name', 'ShortAlias');
 
$shortAliasInstance = new ShortAlias();
var_dump( $shortAliasInstance instanceof Irrational_Long_Class_Name);
# true
var_dump( $shortAliasInstance instanceof ShortAlias);
# true

I was initially worried about behavior of get_class function, but everything works as expected – and get_class returns original class name.

class Irrational_Long_Class_Name
{
 
	public function getClass()
	{
		print get_class();
	}
}
 
class_alias('Irrational_Long_Class_Name', 'ShortAlias');
 
$aInstanceWithAlias = new ShortAlias();
 
$aInstanceWithAlias->getClass();
#  Irrational_Long_Class_Name
print get_class($aInstanceWithAlias);
# Irrational_Long_Class_Name

Also type hinting is aware of aliases.

class TestClass
{
	public function doSomethingWithShortAliasInstance(ShortAlias $b) { }
}
class_alias('Irrational_Long_Class_Name', 'ShortAlias');
$aInstanceWithAlias = new ShortAlias();
$testClassInstance = new TestClass();
$testClassInstance->doSomethingWithShortAliasInstance($aInstanceWithAlias);
# everything is correct

Namespaces aliasing
As Jani mentioned below – namespaces aliasing also can be solution.

use \Irrational_Long_Class_Name as ShortAlias;
  • Date: June 10, 2011
  • Author: Slawek Lukasiewicz
  • Comments: 6 Comments
  • Category: PHP

Working with date and time in object oriented way

Date and time manipulation in PHP is mostly connected with functions like: date, time or strtotime. They can be sufficient, but if we want to deal with dates like with objects – we can use DateTime class. DateTime class is not only straightforward wrapper for standard functions, it has a lot of additional features – for example timezones.
Continue Reading →