public function addToSwimmingPool(Swimming $animal): void
{
$this->pool[] = $animal;
}
public function addToSwimmingPool($animal): void { ... }
public function addToSwimmingPool(Penguin $animal): void { ... }
try {
$this->loginService->login($login, $password);
} catch(UserNotFoundException $ex) {
return ['message' => 'Invalid credential'];
}
<?php
const TOMATO = 'tomato';
const CARROT = 'carrot';
const APPLE = 'apple';
const POTATO = 'potato';
$basket = [
TOMATO => 15,
POTATO => 10,
CARROT => 5,
APPLE => 16,
];
$salad = [
TOMATO => 3,
POTATO => 2,
CARROT => 1,
];
function calculate(array $basket, array $salad): int
{
$total = max($basket);
foreach ($salad AS $key => $value) {
if ($value === 0) {
continue;
}
$total = min(floor(($basket[$key] ?? 0) / $value), $total);
}
return $total;
}
echo calculate($basket, $salad) . PHP_EOL;
Total: 5
private static function removeQueryString(string $url): string
{
$params = explode('&', $url, 2);
if (strpos($params[0], '=') === false) {
return rtrim($params[0], '/');
}
throw new WrongParamException(); // или какая-то другая логика, например возвращать строку по умолчанию
}
<?php
namespace myNameSpace;
function in_array($value, $arr)
{
return 'Local function';
}
$arr = [1, 2, 3];
$value = 1;
var_dump(in_array($value, $arr));
var_dump(\in_array($value, $arr));
string(14) "Local function"
bool(true)