mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-08-20 16:33:35 +00:00
Глава 34: черновик главы
This commit is contained in:
11
exceptions/attribute_exception.php
Normal file
11
exceptions/attribute_exception.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
class AttributeException extends Exception {
|
||||
public function __construct(
|
||||
$attribute,
|
||||
$message = 'Атрибут %s не определен'
|
||||
)
|
||||
{
|
||||
$message = sprintf($message, $attribute);
|
||||
parent::__construct($message, 1001);
|
||||
}
|
||||
}
|
24
exceptions/catch_base.php
Normal file
24
exceptions/catch_base.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
require_once 'user_own_exceptions.php';
|
||||
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
echo $user->password;
|
||||
}
|
||||
catch(AttribueException $exp)
|
||||
{
|
||||
echo "Исключение: {$exp->getMessage()}<br />";
|
||||
echo "в файле {$exp->getFile()}<br />";
|
||||
echo "в строке {$exp->getLine()}<br />";
|
||||
}
|
||||
catch(Exception $exp)
|
||||
{
|
||||
echo "Исключение: {$exp->getMessage()}<br />";
|
||||
echo "в файле {$exp->getFile()}<br />";
|
||||
echo "в строке {$exp->getLine()}<br />";
|
||||
}
|
24
exceptions/finally.php
Normal file
24
exceptions/finally.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
require_once 'user_own_exceptions.php';
|
||||
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
// echo $user->password;
|
||||
}
|
||||
catch(AttribueException $exp)
|
||||
{
|
||||
echo 'AttribueException-исключение<br />';
|
||||
}
|
||||
catch(PasswordException $exp)
|
||||
{
|
||||
echo 'PasswordException-исключение<br />';
|
||||
}
|
||||
finally
|
||||
{
|
||||
echo 'Эта строка выводится не всегда<br />';
|
||||
}
|
9
exceptions/password_exception.php
Normal file
9
exceptions/password_exception.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
class PasswordException extends Exception {
|
||||
public function __construct(
|
||||
$message = 'Не допускается прямое обращение к свойству password'
|
||||
)
|
||||
{
|
||||
parent::__construct($message, 1002);
|
||||
}
|
||||
}
|
28
exceptions/rethrow.php
Normal file
28
exceptions/rethrow.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
require_once 'user_own_exceptions.php';
|
||||
|
||||
try {
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
echo $user->password;
|
||||
}
|
||||
catch(Exception $exp)
|
||||
{
|
||||
echo 'Exception-исключение ' . get_class($exp) . '<br />';
|
||||
// Передача исключения далее по каскаду
|
||||
throw $exp;
|
||||
}
|
||||
}
|
||||
catch(AttribueException $exp)
|
||||
{
|
||||
echo 'AttribueException-исключение';
|
||||
}
|
||||
catch(PasswordException $exp)
|
||||
{
|
||||
echo 'PasswordException-исключение';
|
||||
}
|
26
exceptions/rethrow_fail.php
Normal file
26
exceptions/rethrow_fail.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
require_once 'user_own_exceptions.php';
|
||||
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
echo $user->password;
|
||||
}
|
||||
catch(Exception $exp)
|
||||
{
|
||||
echo 'ExceptionSQL-исключение ' . get_class($exp) . '<br />';
|
||||
// Передача исключения далее по каскаду
|
||||
throw $exp;
|
||||
}
|
||||
catch(AttribueException $exp)
|
||||
{
|
||||
echo 'AttribueException-исключение';
|
||||
}
|
||||
catch(PasswordException $exp)
|
||||
{
|
||||
echo 'PasswordException-исключение';
|
||||
}
|
@ -14,7 +14,7 @@ class User
|
||||
return $this->$index;
|
||||
}
|
||||
|
||||
public function __set(string $index, string $value)
|
||||
public function __set(string $index, string $value) : void
|
||||
{
|
||||
if (isset($this->$index)) {
|
||||
$this->$index = $value;
|
||||
|
26
exceptions/user_exception.php
Normal file
26
exceptions/user_exception.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private string $email,
|
||||
private string $password,
|
||||
private ?string $first_name = null,
|
||||
private ?string $last_name = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function __get(string $index) : ?string
|
||||
{
|
||||
return $this->$index;
|
||||
}
|
||||
|
||||
public function __set(string $index, string $value) : void
|
||||
{
|
||||
if (isset($this->$index)) {
|
||||
$this->$index = $value;
|
||||
}
|
||||
else {
|
||||
throw new Exception("Атрибут $index не существует");
|
||||
}
|
||||
}
|
||||
}
|
22
exceptions/user_exception_use.php
Normal file
22
exceptions/user_exception_use.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require_once 'user_exception.php';
|
||||
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
$user->var = 100;
|
||||
}
|
||||
catch(Exception $exp)
|
||||
{
|
||||
// Блок обработки исключительной ситуации
|
||||
echo "Исключение: {$exp->getMessage()}<br />";
|
||||
echo "в файле {$exp->getFile()}<br />";
|
||||
echo "в строке {$exp->getLine()}<br />";
|
||||
echo '<pre>';
|
||||
echo $exp->getTraceAsString();
|
||||
echo '</pre>';
|
||||
}
|
21
exceptions/user_own_aceptions_use.php
Normal file
21
exceptions/user_own_aceptions_use.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
require_once 'user_own_exceptions.php';
|
||||
|
||||
try {
|
||||
$user = new User(
|
||||
'igorsimdyanov@gmail.com',
|
||||
'password',
|
||||
'Игорь',
|
||||
'Симдянов');
|
||||
|
||||
echo $user->password;
|
||||
}
|
||||
catch(Exception $exp)
|
||||
{
|
||||
echo "Исключение: {$exp->getMessage()}<br />";
|
||||
echo "в файле {$exp->getFile()}<br />";
|
||||
echo "в строке {$exp->getLine()}<br />";
|
||||
echo '<pre>';
|
||||
echo $exp->getTraceAsString();
|
||||
echo '</pre>';
|
||||
}
|
39
exceptions/user_own_exceptions.php
Normal file
39
exceptions/user_own_exceptions.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
require_once 'attribute_exception.php';
|
||||
require_once 'password_exception.php';
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private string $email,
|
||||
private string $password,
|
||||
private ?string $first_name = null,
|
||||
private ?string $last_name = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function __get(string $index) : ?string
|
||||
{
|
||||
if($index == 'password') {
|
||||
throw new PasswordException;
|
||||
}
|
||||
if (isset($this->$index)) {
|
||||
return $this->$index;
|
||||
} else {
|
||||
throw new AttributeException($index);
|
||||
}
|
||||
}
|
||||
public function __set(string $index, string $value) : void
|
||||
{
|
||||
if (isset($this->$index)) {
|
||||
$this->$index = $value;
|
||||
}
|
||||
else {
|
||||
throw new AttributeException($index);
|
||||
}
|
||||
}
|
||||
public function isPasswordCorrect($password)
|
||||
{
|
||||
return $this->password == $password;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user