Showing posts with label abstract. Show all posts
Showing posts with label abstract. Show all posts

Friday 22 November 2013

OOP Series : Abstraction

Abstract classes are particularly useful if you are working in a team and on a large project. They allow the architect to specify what needs to be available in child classes without saying how. They can also be used to specify methods which must be available to all child classes. Abstract classes should't really be called, just referenced. In the example below I have an abstract class of book and a child class of childrensbook. The good news for me in writing the blog post is that there is no abstract class implementation in JavaScript.
PHP : book.class.php
<?php
/* You may only have child classes of this class */
abstract class book
{
function cover()
{
/* All child classes must have this function */
return 'Nice bright cover';
}

/* Must exists in a child class, but you can implement it as you see fit */
abstract function pages();
}
?>
PHP : childrensbook.class.php
<?php
require_once 'book.class.php';
class childrensbook extends book
{
function pages()
{
return 20;
}
}
?>
Now a file (index.php) which creates a childrensbook object.
<?php
require_once 'childrensbook.class.php';
$achildrensbook = new childrensbook;
echo $achildrensbook->cover().'<br />';
echo $achildrensbook->pages().'<br />';
?>