Skip to content

Instantly share code, notes, and snippets.

@jpartogi
Last active October 17, 2015 14:28
Show Gist options
  • Save jpartogi/be4a66cb8c1597bffed9 to your computer and use it in GitHub Desktop.
Save jpartogi/be4a66cb8c1597bffed9 to your computer and use it in GitHub Desktop.
Bad OO Design in PHP
<?php
/**
* @BankAccount.php
* BankAccount is a domain object (POPO) for bank account holder.
**/
class BankAccount {
private $accountNumber;
private $accountBalance;
const interestRate = 5;
public function __construct($accountNumber) {
$this->accountNumber = $accountNumber;
}
// Do interest calculation business logic
public function calculateInterest(){
return (self::interestRate * $this->accountBalance)/100;
}
// Save money into current account business logic
public function save($amount){
$this->accountBalance += $amount;
}
// Withdraw money from current account business logic
public function withdraw($amount){
$this->accountBalance -= $amount;
}
public function getAccountNumber() {
return $this->accountNumber;
}
public function getAccountBalance() {
return $this->accountBalance;
}
public function setAccountBalance($accountBalance) {
$this->accountBalance = $accountBalance;
}
}
$account = new BankAccount("A1234");
echo $account->getAccountNumber();
$account->setAccountBalance(5000);
echo $account->getAccountBalance();
$account->save(5000);
echo $account->getAccountBalance();
$account->withdraw(5000);
echo $account->getAccountBalance();
echo $account->calculateInterest();
<?php
class Shape {
const CIRCLE = 1;
const RECTANGLE = 2;
const TYPE = 0;
private $radius;
private $width;
private $length;
public function area() {
switch(Shape::TYPE){
case Shape::CIRCLE:
return M_PI * radius;
case Shape::RECTANGLE:
return $width * $length;
default:
return 0;
}
}
public function createCircle($radius) {
$this->radius = $radius;
$this->TYPE = Shape::CIRCLE;
}
public function createRectangle($width, $length){
$this->width = $width;
$this->length = $length;
$this->TYPE = Shape::RECTANGLE;
}
}
<?php
interface Shape {
public function area();
}
class Rectangle implements Shape {
private $width;
private $length;
public function __construct($width, $length){
$this->width = $width;
$this->length = $length;
}
public function area(){
return $this->width * $this->length;
}
}
class Square extends Rectangle {
private $edge;
public function __construct($edge) {
super($edge, $edge);
$this->edge = $edge;
}
}
<?php
interface Vehicle {
function fly();
function drive();
}
class Car implements Vehicle {
public function fly() {
throw new Exception("Uh oh cannot fly");
}
public function drive() {
// implement drive here
}
}
class Plane implements Vehicle {
public function fly() {
// implement fly here
}
public function drive() {
throw new Exception("not good for driving");
}
}
$plane = new Plane;
$plane->fly();
$plane->drive();
<?php
interface IOutputter {
function println($output);
}
class ConsoleOutputter implements IOutputter {
public function println($output){
echo $output;
}
}
class MockOutputter implements IOutputter {
public function println($output){
// do nothing
}
}
class CommandInvoker {
public function invoke(ConsoleOutputter $outputter) {
$outputter->println("Hello World");
}
}
$outputter = new ConsoleOutputter;
$invoker = new CommandInvoker;
$invoker->invoke($outputter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment