class API
{
public function getProduct(int $productId): Product
{
$product = $this->request(...); // Получение данных из API
return Product::from($product)
{
}
class Product
{
private int $id;
private string $title;
// прочие свойства
public function getId(): int
{
return $this->id;
}
public function getTitle(): int
{
return $this->title;
}
// прочие геттеры
// сеттеры, если нужны
public static function from(object $source): self
{
$product = new static();
$product->id = $source->id;
$product->title = $source->title;
// заполнение свойств
return $product;
}
}
...
$api = new API($login, $password);
$product = $api->getProduct(325);
$foo = ['order' => '100668327'];
echo $foo['order'];
или$foo = (object)['order' => '100668327'];
echo $foo->order;
$foo = json_decode('{"order":"100668327"}');
echo $foo->order;
или$foo = json_decode('{"order":"100668327"}', true);
echo $foo['order'];
$data = [
['id' => 1, 'field1' => 'Вася'],
['id' => 2, 'field1' => 'Петя'],
['id' => 3, 'field1' => 'Миша']
];
foreach ($data as $row) {
$stmt->execute($row);
}
или$data = [
1 => 'Вася',
2 => 'Петя',
3 => 'Миша'
];
foreach ($data as $id => $field1) {
$stmt->execute(['id' => $id, 'field1' => $field1]);
}
class A
{
private static array $instances = [];
public readonly int $value;
public function __construct(?int $value = null)
{
if ($value !== null) {
$this->value = $value;
static::$instances[] = $this;
}
}
public function summ(): int
{
return array_reduce(
static::$instances,
fn($acc, $cur) => $acc + $cur->value,
0
);
}
}
new A(2);
new A(3);
$summ = (new A())->summ();
print $summ; // 5
$json = file_get_contents('php://input');
$data = json_decode($json);
WITH
`slots` (`slot`) AS (
WITH RECURSIVE
`dates` (`date`, `add`) AS (
SELECT CURDATE(), 1
UNION SELECT `date` + INTERVAL 1 DAY, `add` + 1
FROM `dates`
WHERE `add` < 365
),
`hours` (`hour`, `idx`) AS (
WITH
`c` (`count`) AS (
SELECT LENGTH('02:00,05:00,07:00,08:00') - LENGTH(REPLACE('02:00,05:00,07:00,08:00', ',', '')) + 1
)
SELECT SUBSTRING_INDEX('02:00,05:00,07:00,08:00', ',', 1), 1
UNION SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('02:00,05:00,07:00,08:00', ',', `idx` + 1), ',', -1), `idx` + 1 AS `idx`
FROM `hours`
JOIN `c`
WHERE `idx` < `c`.`count`
)
SELECT CAST(CONCAT(`date`, ' ', `hour`) AS DATETIME) AS `slot`
FROM `dates`
JOIN `hours`
HAVING `slot` > CURTIME()
)
SELECT `s`.`slot`
FROM `slots` AS `s`
LEFT JOIN `autoposting_planned` AS `ap`
ON `ap`.`datetime` = `s`.`slot`
WHERE `ap`.`datetime` IS NULL
ORDER BY `s`.`slot`
LIMIT 1