PHP OOP
By Korvin Szanto
Why use OOP?
- Adds types to values
- Organizes common functions
- Give state to services
Classes
Objects
<?php
class Shirt
{
// Properties
public $color;
public $size;
// Functions
public function getString()
{
$size = $this->size;
$color = $this->color;
return "{$size} {$color} Shirt"
}
// Constructor
public function __construct($color, $size)
{
$this->color = $color;
$this->size = $size;
}
}
<?php
function output_shirt(Shirt $shirt) {
echo $shirt->getString();
}
// Create a new Shirt
$shirt = new Shirt('Blue', 'Large');
// Output the shirt: "Large Blue Shirt"
output_shirt($shirt);
Extending Classes
<?php
class Sweater extends Shirt
{
public $material;
public function getString()
{
$color = $this->color;
$size = $this->size;
$material = $this->material;
return "{$size} {$color} {$material} Sweater";
}
public function __construct($color, $size, $material)
{
$this->material = $material;
// Call Shirt's constructor with the color and the size
parent::__construct($color, $size);
}
}
<?php
function output_shirt(Shirt $shirt) {
echo $shirt->getString();
}
// Create a new Sweater
$shirt = new Sweater('Blue', 'Large', 'Wool');
// Output the shirt: "Large Blue Wool Sweater"
output_shirt($shirt);
Interfaces
<?php
interface ClothingInterface
{
public function getString();
}
<?php
class ClothingOutputter
{
public function outputClothing(ClothingInterface $clothing)
{
echo $clothing->getString();
}
}
<?php
class LargeBlueJeans implements ClothingInterface
{
public function getString()
{
return "Large Blue Jeans";
}
}
<?php
interface StoreItemInterface
{
/**
* Get the price of a store item
* @return float The price, 10.0 for $10.00, 9.58 for $9.58
*/
public function getPrice();
}
<?php
class LargeBlueJeans implements ClothingInterface, StoreItemInterface
{
public function getString()
{
return "Large Blue Jeans";
}
/**
* Get the price of a store item
*
* @return float The price in dollars and cents, Ex: 10.0 for $10.00, 9.58 for $9.58
*/
public function getPrice()
{
return 12.0;
}
}
<?php
interface TaxExemptStoreItemInterface
{
}
<?php
interface CartInterface
{
/**
* Get the total price of this cart
* @return float
*/
public function getTotal();
/**
* Get the items in this cart
* @return StoreItemInterface[]
*/
public function getItems();
/**
* Get the items in this cart
* @return bool
*/
public function addItem(StoreItemInterface $store_item);
}
Traits
<?php
trait TotalingStoreItemListTrait
{
/**
* @return StoreItemInterface[]
*/
abstract public function getItems();
/**
* Get the total price of this item list
*/
public function getTotal()
{
$items = $this->getItems();
$total = 0;
foreach ($items as $item) {
$total += $this->getPriceForItem($item);
}
return $total;
}
/**
* Get the price of an item
*/
protected function getPriceForItem(StoreItemInterface $item)
{
return $item->getPrice();
}
}
<?php
trait TaxableTotalingStoreItemListTrait
{
use TotalingStoreItemListTrait;
protected $flat_tax_rate = 1.0;
/**
* Get the price of an item
*/
protected function getPriceForItem(StoreItemInterface $item)
{
$price = $item->getPrice();
if (!$item instanceof TaxExemptStoreItemInterface) {
$price *= $this->flat_tax_rate;
}
return $price;
}
}
<?php
class Cart implements CartInterface
{
use TaxableTotalingStoreItemListTrait;
protected $items = [];
protected $flat_tax_rate = 1.5;
/**
* Get the items in this cart
* @return StoreItemInterface[]
*/
public function getItems()
{
return $this->items;
}
/**
* Get the items in this cart
* @return bool
*/
public function addItem(StoreItemInterface $store_item)
{
$this->items[] = $store_item;
}
}
<?php
trait StoreItemListTrait
{
protected $store_item_list_items = [];
/**
* Get the items in this cart
* @return StoreItemInterface[]
*/
public function getItems()
{
return $this->store_item_list_items;
}
/**
* Get the items in this cart
* @return bool
*/
public function addItem(StoreItemInterface $store_item)
{
$this->store_item_list_items[] = $store_item;
}
}
<?php
class Cart implements CartInterface
{
use StoreItemListTrait, TaxableTotalingStoreItemListTrait;
protected $flat_tax_rate = 1.5;
}
Namespaces
<?php
namespace Acme\Depot\Store\Item\List;
use Acme\Depot\Store\Item\TaxExemptInterface;
trait TaxableTotalingTrait
{
use TotalingTrait;
protected $flat_tax_rate;
public function setTaxRate($rate)
{
$this->flat_tax_rate = $rate;
}
/**
* Get the price of an item
*/
protected function getPriceForItem(StoreItemInterface $item)
{
if ($item instanceof TaxExemptInterface) {
return $item->getPrice();
} else {
return $item->getPrice * $this->flat_tax_rate;
}
return $item->getPrice();
}
}
<?php
namespace Acme\Depot\Store;
use Acme\Depot\Store\Item\List\TaxTotalingTrait;
use Acme\Depot\Store\Item\List\ListTrait;
// Use vendor cart interface
use Vendor\Store\Cart\CartInterface;
class Cart implements CartInterface
{
use ListTrait, TaxableTotalingTrait;
protected $flat_tax_rate = 1.5;
}
Thanks!
PHP OOP
By Korvin Szanto
PHP OOP
- 496