Skip to content

Instantly share code, notes, and snippets.

@varfrog
Created August 2, 2021 22:30
Show Gist options
  • Save varfrog/594d88467322decd9aa75ef2497f702f to your computer and use it in GitHub Desktop.
Save varfrog/594d88467322decd9aa75ef2497f702f to your computer and use it in GitHub Desktop.
<?php
class BalanceSheet
{
public function __construct(public int $liabilities, public int $equity)
{
}
}
class AssetsCalculator
{
public function calculateAssets(BalanceSheet $balanceSheet): int
{
return $balanceSheet->liabilities + $balanceSheet->equity;
}
}
$assetsCalculator = new AssetsCalculator();
/**
* The following is the different way to achieve the same result as in transform-demo.clj.
* In OOP, we first create an empty collection structure (an empty vector, list or array in PHP) and when we mutate it
* by adding elements to it.
*/
$assetList = [];
foreach (
[
'1950' => new BalanceSheet(7000, 15000),
'1951' => new BalanceSheet(14000, 100000),
'1953' => new BalanceSheet(45000, 350000),
] as $year => $sheet
) {
$assetList[] = $assetsCalculator->calculateAssets($sheet);
}
echo join(', ', $assetList) . PHP_EOL;
# 22000, 114000, 395000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment