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

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

Mobile Device Mean

What is the use of mobile devices? Mobile phones are used for various purposes, such as keeping in touch with family members, conducting business, and having access to a telephone in an emergency. Some people carry more than one mobile phone for different purposes, such as for business and personal use. What Does Mobile Device Mean? A mobile device is a handheld tablet or another device made for portability and is therefore both compact and lightweight. New data storage, processing and display technologies have allowed these small devices to do nearly anything that had previously been traditionally done with larger personal computers. Mobile devices are also known as handheld computers. What are the five uses of mobile phones? In brief, the survey found that the top 5 activities for smartphone users are: Accessing local information. Searching for information. Participating on social media/networking sites. Reading news and entertainment. Finding local services.

Orasaun Ruma Applications

 About my Orasaun Ruma Applications