Serverside Webscripting [JLW322]

04.classes

Examples

Watch and learn ...

Basic Class

<?php

class BasicClass {

	private $var1;
	protected $var2;
	public $var3;

	public function __construct($var) {
		$this->var1 = $var;
		$this->var2 = $var;
		$this->var3 = $var;
	}

	public function doubleVar3() {
		$this->var3 = $this->var3 . ' ' . $this->var3;
	}

	public function getVar1() {
		return $this->var1;
	}

}

$inst = new BasicClass('foo');
echo $inst->getVar1() . PHP_EOL;
$inst->doubleVar3();
echo $inst->var3;

Constants

<?php

class BasicClass {

	const CLASS_CONSTANT = 12.7;

	public function multiply($x) {
		return $x * self::CLASS_CONSTANT;
	}

}

echo BasicClass::CLASS_CONSTANT . PHP_EOL;

$inst = new BasicClass();
echo $inst->multiply(3) . PHP_EOL;

Static Variables & Static Methods

<?php

class BasicClass {

	static $staticVariable;

	public function __construct($name) {
		self::$staticVariable = $name;
	}

	public static function staticFunction($ohai) {
		echo $ohai . PHP_EOL;
	}

}

BasicClass::staticFunction('O Hi');

$inst = new BasicClass('foo');
$inst2 = new BasicClass('bar');

echo $inst::$staticVariable . PHP_EOL;
echo $inst2::$staticVariable . PHP_EOL;

Inheritance (1)

<?php

class Animal {

	private $name;

	public function __construct($name) {
		$this->name = $name;
	}

	protected function say($what) {
		return $what;
	}

}

class Dog extends Animal {

	public function bark() {
		echo $this->say('WOOF!');
	}

}

$dog = new Dog('Sparky');
$dog->bark();

Inheritance (2)

<?php

class Animal {

	private $name;

	public function __construct($name) {
		$this->name = $name;
	}

	final function getName() { // cannot be overridden!
		echo $this->name;
	}
}

class Dog extends Animal {

	public function __construct($name) {
		echo 'Yo dawg!' . PHP_EOL;
		parent::__construct($name); // call parent function
	}

}

$dog = new Dog('Sparky');
echo $dog->getName() . PHP_EOL;

Inheritance (3)

<?php

abstract class Animal {

	private $name;

	public function __construct($name) {
		$this->name = $name;
	}

	abstract public function doTrick();

}

class Dog extends Animal {

	private $tricks = array('jump', 'lay down', 'roll over', 'play dead');

	public function doTrick() {
		return $this->tricks[rand(0, sizeof($this->tricks) - 1)];
	}

}


$dog = new Dog('Sparky');
echo $dog->doTrick();

Summary

The examples in words

Classes Summary (1)

  • Class basics
    • Define a class with the class keyword
    • Create an instance with the new keyword
  • Variables / methods
    • Define a method with the function keyword
    • Defining a variable requires no specific keyword
    • Scope is public, protected, or private
    • Accessible/Callable via $this->dataMember (internal) or
      $instance->dataMember (external, if scope allows it)
  • Constructor and destructor are Magic Methods
    • __construct() and __destruct()

Classes Summary (2)

  • Class constants
    • Use the const keyword and immediately assign a value
    • No $ for name and ALLCAPS
    • Accessible via self::SOME_CONSTANT (internal) or ClassName::SOME_CONSTANT (external)
    • Value cannot be changed at runtime
  • Static variables and methods
    • Use the (extra) static keyword
    • Static variables' value shared amongst instances
    • Static methods callable without an instance
    • Accessible/Callable via ClassName::$someStaticVariable / ClassName::someStaticMethod()

Classes Summary (3)

  • Inheritance
    • Defined with the extends keyword
    • Overriding methods must have the same, or a less restrictive scope
    • Call parent class methods with parent::nameOfTheMethod() (even if it's a non-static method!)
  • Final classes and methods
    • Cannot be extended / overridden
    • Defined with the (extra) final keyword
  • Abstract classes and methods
    • Defined with the (extra) abstract keyword
    • Cannot be instantiated / called; Must be implemented in the derivative class / methods
    • If a class contains an abstract method, the class itself must be abstract too

Want more?

Not talked about

  • Interfaces
    • Define an interface with the interface keyword
    • A class can implement an interface via the implements keyword
    • A class can implement multiple interfaces
    • An interface itself cannot be instantiated
  • Multiple Inheritance
    • Not supported by PHP
    • Use traits to reduce some limitations of single inheritance
  • Namespaces
    • Available since PHP 5.3.0
    • Provides a way to group related classes, interfaces, functions and constants

Questions?

Sources

ikdoeict.be