-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_4.php
38 lines (28 loc) · 860 Bytes
/
2_4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
// THE CONSTRUCTOR & DESTRUCTOR
class User {
//Properties
public $name;
public $age;
//Constructor - Runs when an object is instantiated
public function __construct($name, $age){
echo 'Class '. __CLASS__ . ' instantiated <br>'; //__CLASS__ outputs name of current class
$this->name = $name;
$this->age = $age;
}
//Destructor - Runs when there are no longer any references to any object of this class
// Used for clean up, closing connections etc.
public function __destruct(){
echo 'Destructor ran....<br>';
}
//Methods
public function sayHello(){
return $this->name.' is '. $this->age .' years old and Says Hello'.'<br><br>';
}
}
$user1 = new User('Bob', 29);
echo $user1->sayHello();
$user2 = new User('Sara', 25);
echo $user2->sayHello();
$user3 = new User('Pete', 35);
echo $user3->sayHello();