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

From mi-linux
Jump to navigationJump to search
Line 27: Line 27:
 
$db_server = Preferences::get("db_server");
 
$db_server = Preferences::get("db_server");
 
echo $db_server;
 
echo $db_server;
 +
</pre>
 +
 +
== Abstract classes - an example ==
 +
 +
An abstract class cannot be instantiated, i.e. you cannot use the “new” operator to create objects.
 +
 +
An abstract class defines (and optionally partially implements) the interface for any class that might extend it (via inheritance).
 +
 +
<pre>
 +
abstract class Shape
 +
{
 +
  protected $height;
 +
  protected $width;
 +
 
 +
  public function __construct($height, $width)
 +
  {
 +
    $this->height = $height;
 +
    $this->width = $width;
 +
  }
 +
 
 +
  abstract public function CalculateSurface();
 +
}
 
</pre>
 
</pre>

Revision as of 14:38, 11 September 2009

Main Page >> Web Application Development >> Workbook >> Week 03

Static classes - An example

class Preferences
{
  private static $values = array();
  
  static function set($key, $val)
  {
    self::$values[$key] = $val;
  }
  
  static function get($key)
  {
    return self::$values[$key];
  }
}
Preferences::set("db_server", "mi-linux");
$db_server = Preferences::get("db_server");
echo $db_server;

Abstract classes - an example

An abstract class cannot be instantiated, i.e. you cannot use the “new” operator to create objects.

An abstract class defines (and optionally partially implements) the interface for any class that might extend it (via inheritance).

abstract class Shape
{
  protected $height;
  protected $width;
  
  public function __construct($height, $width)
  {
    $this->height = $height;
    $this->width = $width;
  }
  
  abstract public function CalculateSurface();
}