PHP Coding; bad coding styles



Hey folks,

Before I go on, this isn’t a rant its a learning experience… I swear 😉

PHP is one of those languages that has a very relaxed nature to it, this allows the novice people to pick it up quickly and generally get the work smacked out faster than others.

But as the great comic book writer Stan lee once said “With great power comes great responsibility”…

The reason I use this quote is mainly due to the fact that with the great power and flexibility PHP provides you; you will need to be time and time again reminded of coding best practices and the standards that entwine any programing language, even more so the methodologies and paradigms used by you.

One of these problems surfaces itself brutally in OOP (Object Orientated Programming).

The primary reason coding practices and best practice are important in OOP is due to the nature of the methodology.

OOP is made to extend the functionality of the core language, the ability to abstract yourself from the constraints that surround procedural based programming; to branch into a realm of flexibility that paves the way for the holy grail of programming…. code recycling.

Don’t you remember the days in procedural that you had to type / retype that function or block of code!

So today I am sharing with you all one of my pet hates when it comes to PHP… not using polymorphism when you can!

When reviewing code, I see this alot: (note: I am writing examples as I plan to make this as simple as possible)

class person{
private $name;

private $lives;

public function setName($name)    {

$this->name = $name;

return true;

}

public function setCity($city)    {

$this->lives = $city;

return true;

}

public function getInfo()    {

return “The person: “.$this->name.” is living in “.$this->lives;

}

}

So this in my opinion is a bad use of the class, you now use the class like:

$person = new person();

$person->setName(‘Karl’);

$person->setCity(‘Sydney’);

echo $person->getInfo();

This kinda sucks in my opinion, so many lines to reuse, your retyping the object all the time!

Lets try this with a very simple change to your class:

class person{
private $name;

private $lives;

public function setName($name)    {

$this->name = $name;

return $this;

}

public function setCity($city)    {

$this->lives = $city;

return $this;

}

public function getInfo()    {

return “The person: “.$this->name.” is living in “.$this->lives;

}

}

 

Now all we have done is change those return true’s to return $this.

$this is actually the class information / data as an object, we can pass this back to the functions and use it to call the next method:

$person = new person();

echo $person->setName(‘Karl’)->setCity(‘Sydney’)->getInfo();

As you can see, we have the same final output to the return, but it looks nicer / cleaner and works.

Hey, less typing for you must sometimes be good right?

 

Thanks,

Karl.

 


 


  • Me

    aka fluent interface. Nice to code but not so nice to debug

  • Would you care to elaborate as to why that would be hard to debug?

    Personally I find it very easy to debug, not to mention I will try and throw for any potentials; that is so I can get a stacktrace 🙂

    –Karl.

  • Chris

    May I ask why you chose your setters to be public and not private?

  • because it was an example I didn’t really bother and just defaulted myself to public functions – private vars.