1<?php
2
3// A simple PHP script
4
5namespace App;
6
7use Exception;
8
9class User {
10 private $name;
11 private $email;
12
13 public function __construct($name, $email) {
14 $this->name = $name;
15 $this->email = $email;
16 }
17
18 public function getName() {
19 return $this->name;
20 }
21
22 public function getEmail() {
23 return $this->email;
24 }
25
26 public function save() {
27 // Simulate saving to database
28 echo "Saving user {$this->name} to database.\n";
29 return true;
30 }
31}
32
33function processUsers($users) {
34 foreach ($users as $user) {
35 try {
36 if ($user->save()) {
37 echo "User saved successfully.\n";
38 }
39 } catch (Exception $e) {
40 echo "Error: " . $e->getMessage() . "\n";
41 }
42 }
43}
44
45// Data array
46$users_data = [
47 ['name' => 'Alice', 'email' => 'alice@example.com'],
48 ['name' => 'Bob', 'email' => 'bob@example.com'],
49];
50
51$user_objects = [];
52
53// Loop and create objects
54if (!empty($users_data)) {
55 foreach ($users_data as $data) {
56 $user_objects[] = new User($data['name'], $data['email']);
57 }
58}
59
60// Call function
61processUsers($user_objects);
62
63// Alternative syntax for control structures
64$count = 0;
65while ($count < 3):
66 echo "Count is $count\n";
67 $count++;
68endwhile;
69
70echo "Script completed.\n";
71
72?>