Difference between revisions of "6CC001 Workshop - week 02"

From mi-linux
Jump to navigationJump to search
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Main Page]] >> [[Web Application Development]] >> [[Web Application Developpment - Workbook|Workbook]] >> Week 02
+
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> Week 02
 +
 
 +
We are looking here at the examples already covered in class.
 +
 
 +
First, let's create our superclass:
 +
* The common properties applies to all product types
 +
* The constructor initializes all common properties.
 +
* The getProducer assessor method allows access to the class's properties
 +
* The getProductDetails public method returns a brief summary of the product (title and producer name)
 +
 
 +
<pre>
 +
// ShopProduct.php
 +
class ShopProduct
 +
{
 +
  public $title;
 +
  public $producerMainName;
 +
  public $producerFirstName;
 +
  public $price;
 +
 +
  function __construct($title, $firstName, $mainName, $price)
 +
  {
 +
    $this->title = $title;
 +
    $this->producerFirstName = $firstName;
 +
    $this->producerMainName = $mainName;
 +
    $this->price = $price;
 +
  }
 +
 
 +
  public function getProducer()
 +
  {
 +
    return "$this->producerFirstName
 +
            $this->producerMainName";
 +
  }
 +
 
 +
  public function getProductDetails()
 +
  {
 +
    $s = "$this->title ($this->producerFirstName $this->producerMainName)";
 +
    return $s;
 +
  }
 +
}
 +
</pre>
 +
 
 +
Let’s now create a couple of classes that extend our superclass…
 +
 
 +
First, a CDProduct class:
 +
* It adds the “play length” property.
 +
* It then overrides the constructor. It first calls the parent constructor to initialize the common properties, and then initializes the “play length” property.
 +
* It adds a new assessor method, for the new “Play length” property.
 +
* It also overrides the getProductDetails method, to add the new “play length” property to the product description.
 +
 
 +
<pre>
 +
// CdProduct.php
 +
 
 +
require_once("ShopProduct.php");
 +
 
 +
class CdProduct extends ShopProduct
 +
{
 +
  public $playLength;
 +
 
 +
  // Let's override the constructor
 +
  function __construct($title, $firstName, $mainName, $price, $playLength)
 +
  {
 +
    parent::__construct($title, $firstName, $mainName, $price);
 +
    $this->playLength = $playLength;
 +
  }
 +
 
 +
function getPlayLength()
 +
  {
 +
    return $this->playLength;
 +
  }
 +
 
 +
  // Let's override getProductDetails
 +
  function getProductDetails()
 +
  {
 +
    $s = parent::getProductDetails();
 +
    $s.= " - playing time : $this->playLength";
 +
    return $s;
 +
  }
 +
}
 +
</pre>
 +
 
 +
Likewise let's create a BookProduct class:
 +
* It adds the “number of pages” property.
 +
* It then overrides the constructor. It first calls the parent constructor to initialize the common properties, and then initializes the “number of pages” property.
 +
* It adds a new assessor method, for the new “Number of pages” property.
 +
* It also overrides the getProductDetails method, to add the new “number of pages” property to the product description.
 +
 
 +
<pre>
 +
// BookProduct.php
 +
 
 +
require_once("ShopProduct.php");
 +
 
 +
class BookProduct extends ShopProduct
 +
{
 +
  public $numPages;
 +
 
 +
  // Let's override the constructor
 +
  function __construct($title, $firstName, $mainName, $price, $numPages)
 +
  {
 +
    parent::__construct($title, $firstName, $mainName, $price);
 +
    $this->numPages = $numPages;
 +
  }
 +
 
 +
function getNumberOfPages()
 +
  {
 +
    return $this->numPages;
 +
  } 
 +
 
 +
  // Let's override getProductDetails
 +
  function getProductDetails()
 +
  {
 +
    $s = parent::getProductDetails();
 +
    $s.= " - number of pages : $this->numPages";
 +
    return $s;
 +
  } 
 +
}
 +
</pre>
 +
 
 +
All done! Now let's test:
 +
 
 +
<pre>
 +
// test.php
 +
 
 +
require_once("CdProduct.php");
 +
require_once("BookProduct.php");
 +
 
 +
$CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63);
 +
$CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51);
 +
 
 +
$Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
 +
$Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);
 +
 
 +
echo $CD1->getProductDetails()."<br>";
 +
echo $CD2->getProductDetails()."<br>";
 +
echo $Book1->getProductDetails()."<br>";
 +
echo $Book2->getProductDetails()."<br>";
 +
</pre>
 +
 
 +
== Try it ==
 +
 
 +
Try creating and using new properties and methods in the classes above.

Latest revision as of 13:24, 21 January 2012

Main Page >> Advanced Web Technologies >> Workbook >> Week 02

We are looking here at the examples already covered in class.

First, let's create our superclass:

  • The common properties applies to all product types
  • The constructor initializes all common properties.
  • The getProducer assessor method allows access to the class's properties
  • The getProductDetails public method returns a brief summary of the product (title and producer name)
// ShopProduct.php
class ShopProduct
{
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
 
  function __construct($title, $firstName, $mainName, $price)
  {
    $this->title = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price = $price;
  }

  public function getProducer()
  {
    return "$this->producerFirstName 
            $this->producerMainName";
  }

  public function getProductDetails()
  {
    $s = "$this->title ($this->producerFirstName $this->producerMainName)";
    return $s;
  }
}

Let’s now create a couple of classes that extend our superclass…

First, a CDProduct class:

  • It adds the “play length” property.
  • It then overrides the constructor. It first calls the parent constructor to initialize the common properties, and then initializes the “play length” property.
  • It adds a new assessor method, for the new “Play length” property.
  • It also overrides the getProductDetails method, to add the new “play length” property to the product description.
// CdProduct.php

require_once("ShopProduct.php");

class CdProduct extends ShopProduct
{
  public $playLength;
  
  // Let's override the constructor
  function __construct($title, $firstName, $mainName, $price, $playLength)
  {
    parent::__construct($title, $firstName, $mainName, $price);
    $this->playLength = $playLength;
  }

 function getPlayLength()
  {
    return $this->playLength;
  }
  
  // Let's override getProductDetails
  function getProductDetails()
  {
    $s = parent::getProductDetails();
    $s.= " - playing time : $this->playLength";
    return $s;
  }
}

Likewise let's create a BookProduct class:

  • It adds the “number of pages” property.
  • It then overrides the constructor. It first calls the parent constructor to initialize the common properties, and then initializes the “number of pages” property.
  • It adds a new assessor method, for the new “Number of pages” property.
  • It also overrides the getProductDetails method, to add the new “number of pages” property to the product description.
// BookProduct.php

require_once("ShopProduct.php");

class BookProduct extends ShopProduct
{
  public $numPages;
  
  // Let's override the constructor
  function __construct($title, $firstName, $mainName, $price, $numPages)
  {
    parent::__construct($title, $firstName, $mainName, $price);
    $this->numPages = $numPages;
  } 

 function getNumberOfPages()
  {
    return $this->numPages;
  }  
  
  // Let's override getProductDetails
  function getProductDetails()
  {
    $s = parent::getProductDetails();
    $s.= " - number of pages : $this->numPages";
    return $s;
  }  
}

All done! Now let's test:

// test.php

require_once("CdProduct.php");
require_once("BookProduct.php");

$CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63);
$CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51);

$Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
$Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);

echo $CD1->getProductDetails()."<br>";
echo $CD2->getProductDetails()."<br>";
echo $Book1->getProductDetails()."<br>";
echo $Book2->getProductDetails()."<br>";

Try it

Try creating and using new properties and methods in the classes above.