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