phptest - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of phptest.php

  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace Agis2;
  6.  
  7. use Agis2\Notification\Entity\EmailSender;
  8.  
  9. class Auth
  10. {
  11.     public function __construct(private string $username, private string $password)
  12.     {
  13.     }
  14.  
  15.     public function getUsername(): string
  16.     {
  17.         return $this->username;
  18.     }
  19. }
  20.  
  21. class Contact
  22. {
  23.     public function __construct(private string $phone, private string $email)
  24.     {
  25.     }
  26.  
  27.     public function getEmail(): string
  28.     {
  29.         return $this->email;
  30.     }
  31.  
  32.     public function setEmail(string $email): void
  33.     {
  34.         $this->email = $email;
  35.     }
  36.  
  37.     public function getPhone(): string
  38.     {
  39.         return $this->phone;
  40.     }
  41. }
  42.  
  43. class User
  44. {
  45.     public function __construct(private Auth $auth, private Contact $contact)
  46.     {
  47.     }
  48.  
  49.     public function getAuth(): Auth
  50.     {
  51.         return $this->auth;
  52.     }
  53.  
  54.     public function getEmail(): string
  55.     {
  56.         return $this->contact->getEmail();
  57.     }
  58.  
  59.     public function setEmail(string $email): void
  60.     {
  61.         $this->contact->setEmail($email);
  62.     }
  63.  
  64.     public function sendEmail(string $subject, string $message): void
  65.     {
  66.         EmailSender::send($this->getEmail(), $subject, $message);
  67.     }
  68.  
  69.     public function sendSms(string $message): void
  70.     {
  71.         PhoneSender::send($this->contact->getPhone(), $message);
  72.     }
  73.  
  74.     public function createUser(string $username, string $password, string $phone, string $email): self
  75.     {
  76.         return new self(
  77.             new Auth($username, $password),
  78.             new Contact($phone, $email),
  79.         );
  80.     }
  81.  
  82.     public static function parseFile(string $path): \Generator
  83.     {
  84.         $handle = fopen($path, 'r');
  85.  
  86.         while (!feof($handle)) {
  87.             yield trim(fgets($handle));
  88.         }
  89.  
  90.         fclose($handle);
  91.     }
  92.  
  93.     public static function loadFromFile(string $path): array
  94.     {
  95.         $users = [];
  96.  
  97.         foreach (self::parseFile($path) as $row) {
  98.             [$username, $password, $phone, $email] = explode(';', $row);
  99.  
  100.             $users[] = $this->createUser($username, $password, $phone, $email);
  101.         }
  102.  
  103.         return $users;
  104.     }
  105. }
  106.  
File Description
  • phptest
  • PHP Code
  • 11 May-2023
  • 2.06 Kb
You can Share it: