Singleton
class Singleton
{
private static
$instance = null;
public static function getInstance()
{
if (null === self::$instance)
{
self::$instance = new self();
}
return self::$instance;
}
private function __clone() {}
private function __construct() {}
}
Обработка и вызов ошибки
class Word
{
protected int $n;
public function getInt($n){
$this->n=$n;
try {
if ($this->n == 0) {
throw new Exception("0 is not divided");
} else if ($this->n % 2 == 0) {
return 'true';
} else {
return 'false';
}
}
catch(Exception $e){
echo $e->getMessage().$e->getLine();
}
}
}
$mir=new Word();
echo $mir->getInt(3);
echo $mir->getInt(2);
echo $mir->getInt(0);
Класс Builder
<?php
namespace data;
use http\Exception\InvalidArgumentException;
class Builder
{
private array $select = ['*'];
private ?string $from = null;
private array $where = [];
private array $andWhere = [];
private array $orWhere = [];
private array $orderBy = [];
public function select(array $select): self
{
if (empty($select)) {
throw new InvalidArgumentException('$select не может быть пустым');
}
$this->select = $select;
return $this;
}
public function from(string $tableName): self
{
if (empty($tableName)) {
throw new InvalidArgumentException('$tableName не может быть пустым');
}
$this->from = $tableName;
return $this;
}
public function where(array $conditions): self
{
$this->where = $conditions;
return $this;
}
public function andWhere(array $conditions): self
{
$this->andWhere = $conditions;
return $this;
}
public function orWhere(array $conditions): self
{
$this->orWhere = $conditions;
return $this;
}
public function orderBy(array $conditions): self
{
$this->orderBy = $conditions;
return $this;
}
public function getQuery(): string
{
if (empty($this->from)) {
throw new \Exception('Не установлено значение $from');
}
$query = '';
$query .= 'SELECT ' . implode(',', $this->select);
$query .= ' FROM ' . $this->from;
if (!empty($this->where)) {
if (count($this->where) > 1) {
$i = 0;
foreach ($this->where as $condition) {
if ($i == 0) {
$query .= " WHERE $condition";
} else {
$query .= " AND $condition";
}
}
} else {
$colName = array_key_first($this->where);
$query .= " WHERE {$this->where[$colName]}";
}
if (!empty($this->andWhere)) {
foreach ($this->andWhere as $condition) {
$query .= " AND $condition";
}
}
if (!empty($this->orWhere)) {
foreach ($this->orWhere as $condition) {
$query .= " OR $condition";
}
}
}
if (!empty($this->orderBy)) {
$query .= " ORDER BY " . implode(',', $this->orderBy);
}
return $query;
}
}
Соединение между PHP и сервером базы данных.
namespace data;
use PDO;
class DB
{
private static $_instance = null;
private static $_connection;
private const DB_HOST = 'localhost';
private const DB_NAME = 'itvdn';
private const DB_USER = 'root';
private const DB_PASS = 'password';
private function __construct()
{
static::$_connection = new PDO(
'mysql:host=' . self::DB_HOST . ';dbname=' . self::DB_NAME,
self::DB_USER,
self::DB_PASS,
[PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]
);
}
private function __clone()
{
}
private function __wakeup()
{
}
public static function getInstance(): self
{
if (!static::$_instance) {
static::$_instance = new static();
}
return static::$_instance;
}
public function getConnection(): PDO
{
return static::$_connection;
}
}