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

Konstituisaun RDTL App

Konstituisaun RDTL App Livru manual konstituisaun RDTL ne’ebe mak Estadu Timor Leste Aprova iha 2002 to’o ohin loron membru Governu hanaran Lei Inan atu sidadaun Timor Leste bele lao tuir no banati tuir. Maibe livru manual ida ne’e seidauk sufsiente ba timor oan hotu atu lé no kompriende diak konaba kostituisaun ida ne’e tantu iha rai laran no mos iha rai liur tamba menus husi livru Digitalizasaun. Ho desafius hirak ne’ebe mak ita hasoru ona  Hau mai ho solusaun ida kria Aplikasaun Konstituisaun RDTL  offline ida  atu nune’e ba ita iha ne’ebe deit bele asesu iha telemovel rasik para hodi labele lakon tan, no hodi ita la presija lori tun sae Kaner wanhira ita presija, no ba maluk sira iha railaran laran no mos iha rain Liur no mos maluk Estrangeiru sira fasil atu utiliza no hodi bele hatene ita nia konstituisaun para ser labele halo arbiru iha ita nia rai doben Timor Lorosae. Termu Privasidade ba Applikasaun At...

Aplikasaun Android Hahi Ba Nai Maromak (Protestante)

Maneira Utiliza Aplikasaun Android Hahi Ba Nai Maromak Introdusaun Hahi Ba Nai Maromak ho Android ne'e bele tulun ba ita hotu nu'dar reinu Maromak nian iha Timor-Leste liuliu ba membru Igreja Protestante iha Timor Lorosa’e (IPTL) iha rai laran no rai liur bele asesu ho diak ba HBM (Hahi Ba Nai Maromak). Livru hananu ne'e, kria no tradus husi membru Igreja Protestante Iha Timor Lorosa'e (IPTL). Ho numeru kantiku numeru 172, hamutuk ho Maromak Nia Ukun Fuan Boot, Maromak Nia Ukun Fuan Sanulu, Orsaun Ami Aman, Kredo Apostulado no Kredo Nicea Konstantinopel.   Hahu Utiliza Aplikasaun 1.      Primeiru utilizador klik iha icon aplikasaun ne’ebé instal ona iha telemovel android. Hanesan imajen tuir mai ne’e :   Imajen 1. Icon Aplikasaun 2.      Tuir mai sei mosu tampilan welcome husi aplikasaun nian molok fo sai menu sira husi aplikasaun hanesan imajen tuir mai ne’e :   Imajen 2. Splash Screen 3.      Bainhira remata ti...