Class

Posted in PHP by

Classes represent Objects with attributes and actions. Object Oriented Programming (OOP)is possible since the introduction of PHP5. The latest version of PHP gives us Late-Static-Binding and a bunch of magic methods.

What are classes and why are they so awesome? Classes allow you to group functions and variables together. The benefits are a more structured code, better readability and classes are easy to maintain. This example will show you the basics and the power behind classes (that makes em awesome)

<php
class Helper{
public static function loremIpsum(){
  return 'loremipsum...';
}
}
class Base{
protected $title;
public function doTitle(){
  $this->title = 'Awesome'; 
}
}
class Page extends Base{
 protected $title;
 private $articleBody;
public construct(){
  $this->doTitle();//doTitle from Base is executed. But the title is still in $this->title.
  $this->articleBody = Helper::loremIpsum();//static functions can be used always.
}
public function render(){
 return '

'.$this->title.'

'.$this->articleBody.'

'; } } $page = new Page();//calls
construct in Page. echo $page->render();//can be called because declared public.

Published at , Updated at 2011-10-25

next: user friendly urls prev: Namespace