Session 6 - PHP
Session 6 - PHP
PROGRAMMING I
Session 6 – PHP
Slide 2
Abstract Classes
• An abstract class cannot be instantiated.
• Instead it defines (and, optionally, partially implements) the
interface for any class that might extend it.
• You define an abstract class with the abstract keyword.
abstract class ProductWriter
{
protected $products = [];
public function addProduct(Product $product)
{
$this->products[] = $product;
}
}
Slide 3
PHP Abstract Classes
• In most cases, an abstract class will contain at least one abstract method.
• These are declared, once again, with the abstract keyword.
abstract class ProductWriter
{
protected $products = [];
public function addProduct(Product $product)
{
$this->products[]=$product;
}
Slide 4
PHP Interfaces
• Although abstract classes let you provide some
measure of implementation, interfaces are pure
templates.
• An interface can only define functionality; it can
never implement it.
• An interface is declared with the interface keyword.
• It can contain properties and method declarations but
not method bodies.
Slide 5
PHP Interfaces
• Any class that incorporates an interface commits to
implementing all the methods it defines, or it must be
declared abstract
interface Chargeable
{
public function getPrice(): float;
}
Slide 6
Implementing Interfaces
• A class can implement an interface using the implements keyword in its
declaration
class Product implements Chargeable
{
// ...
protected $price;
// ...
public function getPrice(): float
{
return $this->price;
}
// ...
}
Slide 7
Traits
• As we have seen, interfaces help you manage the fact
that, like Java, PHP does not support multiple
inheritance.
• A class in PHP can only extend a single parent
• However, you can make a class promise to implement
as many interfaces as you like; for each interface it
implements, the class takes on the corresponding
type.
• Traits allows you to share an implementation across
inheritance hierarchies
Slide 8
Traits
• A trait is a class-like structure that cannot itself be
instantiated but can be incorporated into classes.
• A trait changes the structure of a class, but doesn’t
change its type.
• Since one of the core object-oriented design goals is
the removal of duplication, Traits allows you to
achieve just that.
Slide 9
Defining a Trait
trait PriceUtilities
{
private $taxrate = 17;
public function calculateTax(float $price)
{
return (($this->taxrate / 100) * $price);
}
// other utilities
}
Slide 10
Using a Trait
class Product
{
use PriceUtilities;
}
Slide 11
Using More than One Trait
• You can include multiple traits in a class by listing
each one after the use keyword, separated by
commas.
class Product
{
use PriceUtilities, IdentityTrait;
}
Slide 12
Combining Traits and Interfaces
• Although traits are useful, they don’t change the type
of the class to which they are applied.
• So when you apply the trait to multiple classes, they
won’t share a type that could be hinted for in a
method signature.
• Luckily, traits play well with interfaces.
Slide 13
Defining Abstract Methods in Traits
• You can define abstract methods in a trait in just the same way you
would in a class.
• When a trait is used by a class, it takes on the commitment to
implement any abstract methods it declares.
trait PriceUtilities
{
function calculateTax(float $price): float
{
return (($this->getTaxRate() / 100) * $price);
}
abstract function getTaxRate(): float;
// other utilities
}
Slide 14
Interceptor Methods
• PHP provides built-in interceptor methods that can
intercept messages sent to undefined methods and
properties
• Like __construct(), they are invoked for you when the
right conditions are met
Slide 15
Interceptor Methods
Slide 16
Copying Objects with __clone()
• In PHP 4, copying an object was a simple matter of
assigning from one variable to another
class CopyMe
{
}
$first = new CopyMe();
$second = $first;
• PHP 4: $second and $first are 2 distinct
objects
• PHP 5 plus: $second and $first refer to one
object
Slide 17
Copying Objects with __clone()
• PHP provides the clone keyword for just this purpose.
clone operates on an object instance, producing a by-
value copy
class CopyMe
{
}
$first = new CopyMe();
$second = clone $first;
• // PHP 5 plus: $second and $first are 2 distinct objects
Slide 18
Copying Objects with __clone()
• You can control what is copied when clone is invoked
on an object.
• You do this by implementing a special method called
__clone()
• __clone() is called automatically when the clone
keyword is invoked on an object
Slide 19
PHP Packages and Namespaces
• A package is a set of related classes, usually grouped
together in some way.
• Packages can be used to separate parts of a system
from one another.
• PHP has no native concept of a package, but as of
PHP 5.3, it introduced namespaces
• Namespaces help us deal with the possibility of
naming collisions
Slide 20
PHP Namespaces
• In essence, a namespace is a bucket in which you can
place your classes, functions, and variables.
• Within a namespace you can access these items
without qualification.
• From outside, you must either import the namespace,
or reference it, in order to access the items it contains.
Slide 21
PHP Namespaces
namespace useful;
class Outputter
{
//
}
Slide 22
Accessing Classes using Namespaces
namespace myspace; class Debug
{
public static function helloWorld()
{
print "hello from myspace\n";
}
}
use myspace\Debug;
Debug::helloWorld();
Slide 23