Closed for holidays
Filed in
Personal
Tags: holidays, japan, summer, travels
Closed for holidays. I’ll be in Japan until 15th August. I think I deserve a rest. Up until then, have a lovely summer.
Filed in
Personal
Tags: holidays, japan, summer, travels
Closed for holidays. I’ll be in Japan until 15th August. I think I deserve a rest. Up until then, have a lovely summer.
Filed in
Computers
Tags: constructors, development, factory method, patterns, php, tip
As you probably know, it’s possible to have multiple constructors in Java. They need to have the same name as the class, and they can only be distinguished by the number and type of arguments. In PHP5, you can only have one constructor. You can define it using the reserved word __construct. If the __construct function doesn’t exist, PHP5 will search for the old-style constructor function (by the name of the class). So if we cannot have multiple constructors, how could we create objects with different initial conditions? It’s not a big deal. There’s a pattern called Factory Method, which defines virtual constructors using static methods. Let’s see an example:
class Person
{
private $name;
private $email;
public static function withName($name)
{
$person = new Person();
$person->name = $name;
return $person;
}
public static function withEmail($email)
{
$person = new Person();
$person->email = $email;
return $person;
}
public static function fullPerson($name, $email)
{
$person = new Person();
$person->name = $name;
$person->email = $email;
return $person;
}
}
We have a class called Person which contains 2 private attributes: name and email. It also has 3 static methods: withName, withEmail and fullPerson. These methods will behave like constructors.
So if we want to create a Person object just with the name value, we can do it using the following statement:
$person = Person::withName('Example');
Filed in
Computers
Tags: development, php, tip
Writing intelligible code helps to increase the productivity of a developing team, even if you are an independent worker. Why is it so important to write intelligible code? How can it improve the productivity of my team? A messed up code could delay your partners understanding, or it could create a barrier for new developers. Even trying to understand your own code could be a real challenge as well. Here are 7 tips of how to make more intelligible PHP code (coding style tips):
Bad
list($foo, $bar, $baz) = array(Zim::getVal('foo'), Dib::getVal('bar'), Gir::getVal('baz', Gir::DOOM));
Good
$foo = Zim::getVal('foo');
$bar = Dib::getVal('bar');
$baz = Gir::getVal('baz', Gir::DOOM);
Bad
$example = 'string_value';
$anotherExample = 42;
$exampleInst = new ExampleClass();
Good
$example = 'string_value';
$anotherExample = 42;
$exampleInst = new ExampleClass();
{block1}
{block2}
{block3}
...
if ($display === 1) {
while ($obj->next()) {
echo $obj->toString();
}
}
class Foo
{
private $_obj;
public function __construct($obj)
{
$this->_obj = $obj;
}
}
BAD
$example = array(Foo::get('key',2),$value,true);
GOOD
$example = array(Foo::get('key', 2), $value, true);
BAD
class Foo
{
public static function getStatus()
{
$res = 0;
if (!self::isValid()) {
$res = 1;
}
...
if ($res === 0 && self::isRepeated()) {
$res = 2;
}
return $res;
}
}
GOOD
class Foo
{
const STATUS_SUCCESS = 0;
const STATUS_FAILURE = 1;
const STATUS_REPEATED = 2;
public static function getStatus()
{
$res = self::STATUS_SUCCESS;
if (!self::isValid()) {
$res = self::STATUS_FAILURE;
}
...
if ($res === self::STATUS_SUCCESS && self::isRepeated()) {
$res = self::STATUS_REPEATED;
}
return $res;
}
}
These tips are just some coding style tips. If we really want to write intelligible code, we would keep in mind many more things like to use software patterns, to make use of well established software practises, …
Filed in
Internet,
Bussiness 2.0
Tags: digg, google, purchase
Has Google acquired Digg for 200 millions dollars? Michael Arrington has written a post on TechCrunch saying that both companies have reached an agreement for around 200 millions dollars. The popular social bookmarking site would become part of Google News property.