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

How To Install Android Studio & SDK Manager & On Windows 10

How To Install Android Studio On Windows 10 Introduction The official Integrated Development Environment (IDE) for developing Android Apps is Android Studio, which Google supports. Java was replaced by kotlin on May 7, 2019, as a preferred language for developing Android Apps. But still, Java is being used for developing Android Apps. Android Studio 3.6.1 has the following features. Gradle-based build support.UI components can be created by drag and drop features in the layout editor. Common Android designs and components can be created by Template-based wizards. Built-in support for Google Cloud Platform enabling integration with Firebase Cloud Messaging (Earlier 'Google Cloud Messaging') and Google App Engine.Lint tools to catch performance, usefulness, version compatibility, and other problems. This tutorial will show you the system requirements and various steps on how to download and install Android Studio on Windows 10. Syste...

Impaktu Transformativu husi IA iha Edukasaun no Empregu: Haree ba Futuru

 Títulu: Impaktu Transformativu husi IA iha Edukasaun no Empregu: Haree ba Futuru Introdusaun  Intelijénsia Artifisiál (IA) la'ós ona konseitu futurista ida; ida-ne'e maka realidade ida ne'ebé maka muda aspetu oioin hosi ita-nia moris, partikularmente iha edukasaun no merkadu traballu. Enkuantu teknolojia sira IA nian kontinua atu evolui, sira promete atu revolusiona oinsá ita aprende, hanorin, no serbisu. Artigu ida-ne'e esplora efeitu potensiál sira hosi IA iha estudu no empregu, hodi destaka oportunidade no dezafiu sira ne'ebé maka iha oin. AI iha Edukasaun: Esperiénsia Aprendizajen Personalizadu  Maneira ida ne'ebé signifikativu liu ne'ebé AI muda edukasaun maka liuhosi aprendizajen personalizadu. Modelu edukasionál tradisionál sira dala barak adota aprosimasaun ida ne'ebé hanesan ba ema hotu, ne'ebé bele husik estudante barak iha kotuk. AI, maibé, bele analiza padraun aprendizajen individuál, forsa, no frakeza sira, hodi permite esperiénsia...