Skip to main content

S.O.L.I.D: The First 5 Principles of Object-Oriented Design with PHP

 


S.O.L.I.D: The First 5 Principles of Object-Oriented Design with PHP


class Logger{
private $logs = [];

public function add($log){
$now = new DateTime();
$date = $now->format("Y-m-d h:i:s.u");
$this->logs[] = $date." : ".$log;
}
public function toString($dimiliter=", "){
if(empty($this->logs)){
return "No logs";
}
return implode($this->logs,$dimiliter);

}
public function reset(){
$this->logger=[];
}
public function save($fileName){
$fp = fopen($fileName,"w");
fwrite($fp,$this->toString("\n"));
fclose($fp);
}

}
$logger = new Logger();
$logger->add("First log");
$logger->add("Second log");
$logger->add("Third log");
$logger->save("logs.txt");
<?php 
class Logger{
private $logs = [];

public function add($log){
$now = new DateTime();
$date = $now->format("Y-m-d h:i:s.u");
$this->logs[] = $date." : ".$log;
}
public function toString($dimiliter=", "){
if(empty($this->logs)){
return "No logs";
}
return implode($this->logs,$dimiliter);

}
public function reset(){
$this->logger=[];
}
public function save($fileName){
$fp = fopen($fileName,"w");
fwrite($fp,$this->toString("\n"));
fclose($fp);
}

}
class LogStorage{
private $fileName;
public function __construct($fileName){
$this->fileName = $fileName;
}
public function save($text){
$fp = fopen($this->fileName,"w");
fwrite($fp,$text);
fclose($fp);
}
}
$logger = new Logger();
$logger->add("First log");
$logger->add("Second log");
$logger->add("Third log");
$logStorage = new LogStorage("pfile.txt");
$logStorage->save($logger->toString("\n"));
<?php
class SavingAccount
{
private $balance;
public function setBalance($balance){}
public function getBalance(){}
public function withdrawal(){}
}
class FixedDipositAccount()
{
private $balance;
private $maturityPeriod;
public function setBalance($balance){}
public function getBalance(){}
}
class IntrestCalculator
{
public function calculate($account)
{
if ($account instanceof SavingAccount) {
return $account->getBalance*3.0;
} elseif ($member instanceof FixDipositAccount) {
return $account->getBalance*9.5;
}
throw new Exception('Invalid input member');
}
}
$savingAccount = new SavingAccount();
$savingAccount->setBalance(15000);
$fdAccount = new FixedDipositAccount();
$fdAccount->setBalance(25000);
$intrestCalculator = new IntrestCalculator();
echo $intrestCalculator->calculate($savingAccount);
echo $intrestCalculator->calculate($fdAccount);
<?php
interface Account
{
public function calculateInterest();
}
class SavingAccount implements Account
{
private $balance;
private $rate=3.0;
private $maturityPeriod;
public function setBalance($balance){}
public function getBalance(){}
public function withdrawal(){}

public function calculateIntrest(){
$this->$rate*$this->balance;
}
}
class FixedDipositAccount implements Account
{
private $balance;
private $rate =9.5;
public function setBalance($balance){}
public function getBalance(){}

public function calculateIntrest(){
$this->$rate*$this->balance;
}
}
class IntrestCalculator
{
public function calculate(Account $account)
{
return $account->calculateIntrest();
}
}
$savingAccount = new SavingAccount();
$savingAccount->setBalance(15000);
$fdAccount = new FixedDipositAccount();
$fdAccount->setBalance(25000);
$intrestCalculator = new IntrestCalculator();
echo $intrestCalculator->calculate($savingAccount);
echo $intrestCalculator->calculate($fdAccount);
Class A
{
public function doSomething(){
}
}
Class B extends A
{

}
<?php
// The Rectangle Square problem
class Rectangle
{
protected $width;
protected $height;
public function setHeight($height)
{
$this->height = $height;
}
public function getHeight()
{
return $this->height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function getWidth()
{
return $this->width;
}
public function area()
{
return $this->height * $this->width;
}
}
class Square extends Rectangle
{
public function setHeight($value)
{
$this->width = $value;
$this->height = $value;
}
public function setWidth($value)
{
$this->width = $value;
$this->height = $value;
}
}
class AreaTester
{
private $rectangle;
public function __construct(Rectangle $rectangle)
{
$this->rectangle = $rectangle;
}
public function testArea($width,$height)
{
$this->rectangle->setHeight($width);
$this->rectangle->setWidth($height);
return $this->rectangle->area();
}
}
$rectangle = new Rectangle();
$rectangleTest = new AreaTester($rectangle);
$rectangleTest->testArea(2,3); // gives 6 as expecated$squre = new Square();
$rectangleTest = new AreaTester($squre);
$rectangleTest->testArea(2,3); // gives 9 expecated is 6
interface LogRepositoryInterface
{
/**
* Gets all logs.
*
* @return array
*/
public function getAll();
}
class FileLogRepository implements LogRepositoryInterface
{
public function getAll()
{
// Fetch the logs from the file and return an array
return $logsArray;
}
}
class DatabaseLogRepository implements LogRepositoryInterface
{
public function getAll()
{
// fetch Logs from model Log and call toArray() function to match the return type.
return Log::all()->toArray();
}
}
<?phpinterface IPrintMachine
{
public function print(Document $d);
public function scan(Document $d);
public function xerox(Document $d);
}
class Document {
// some attributes and methods
}
class AdvancePrinter implements IPrintMachine
{
public function print(Document $d){
echo "Print document";
}
public function scan(Document $d){
echo "Scan document";
}
public function xerox(Document $d){
echo "Take xerox copy of document";
}
}
class SimplePrinter implements IPrintMachine
{
public function print(Document $d){
echo "Print document";
}
public function scan(Document $d){
echo "Not supported";
}
public function xerox(Document $d){
echo "Not supported";
}
}
<?php
interface IPrinter
{
public function print(Document $d);
}
interface IScanner
{
public function scan(Document $d);
}
interface IXerox
{
public function xerox(Document $d);
}
class Document {
// some attributes and methods
}
class AdvancePrinter implements IPrinter,IScanner,IXerox
{
public function print(Document $d){
echo "Print document";
}
public function scan(Document $d){
echo "Sacn document";
}
public function xerox(Document $d){
echo "Take xerox copy of document";
}
}
class SimplePrinter implements IPrinter
public function print(Document $d){
echo "Print document";
}
}
<?php
class MySqlConnection {
public function connect() {}
}

class Post{
private $dbConnection;
public function __construct(MySqlConnection $dbConnection) {
$this->dbConnection = $dbConnection;
$this->dbConnection->connect();
}
}
interface DbConnectionInterface {
public function connect();
}

class MySqlConnection implements DbConnectionInterface {
public function connect() {}
}

class Post {
private $dbConnection;
public function __construct(DbConnectionInterface $dbConnection) {
$this->dbConnection = $dbConnection;
$this->dbConnection->connect();
}
}

Popular posts from this blog

Orasaun Ruma Applications

 About my Orasaun Ruma Applications

Cristo Rei of Dili

  Cristo Rei of Dili   e then Indonesian province. (Christ the King of Dili) is a 27.0-meter-high (88.6 ft) statue of Jesus located atop a globe in Dili, East Timor. The statue was designed by Mochamad Syailillah, who is better known as Boil. The statue was officially unveiled by Soeharto in 1996 as a gift from the Indonesian Government to the people of Timor Timur. The statue, and the globe on which it rests, are situated at the end of the Fatucama peninsula, facing out to the ocean, and can be reached by climbing some 597 steps. Question time  When was Cristo Rei Dili built?                1996                 This 89-foot statue of Cristo Rei was constructed in 1996 as a present from                                     Indonesia to East Timor. ...

Monte de Santo Antonio de Manatuto

Monte de Santo Antonio de Manatuto Manatuto (Portuguese: Município Manatuto, Tetum : Munisípiu Manatutu) is one of the municipalities (formerly districts) of East Timor, located in the central part of the country. It has a population of 45,541 (Census 2010) and an area of 1,783.3 km². The capital of the municipality is also named Manatuto. It is the least populated municipality of East Timor