xref: /PHP-7.4/tests/classes/class_example.phpt (revision d679f022)
1--TEST--
2Classes general test
3--FILE--
4<?php
5
6/* pretty nifty object oriented code! */
7
8class user {
9  public $first_name,$family_name,$address,$phone_num;
10  function display()
11  {
12    echo "User information\n";
13    echo "----------------\n\n";
14    echo "First name:\t  ".$this->first_name."\n";
15    echo "Family name:\t  ".$this->family_name."\n";
16    echo "Address:\t  ".$this->address."\n";
17    echo "Phone:\t\t  ".$this->phone_num."\n";
18    echo "\n\n";
19  }
20  function initialize($first_name,$family_name,$address,$phone_num)
21  {
22    $this->first_name = $first_name;
23    $this->family_name = $family_name;
24    $this->address = $address;
25    $this->phone_num = $phone_num;
26  }
27};
28
29
30function test($u)
31{  /* one can pass classes as arguments */
32  $u->display();
33  $t = $u;
34  $t->address = "New address...";
35  return $t;  /* and also return them as return values */
36}
37
38$user1 = new user;
39$user2 = new user;
40
41$user1->initialize("Zeev","Suraski","Ben Gourion 3, Kiryat Bialik, Israel","+972-4-8713139");
42$user2->initialize("Andi","Gutmans","Haifa, Israel","+972-4-8231621");
43$user1->display();
44$user2->display();
45
46$tmp = test($user2);
47$tmp->display();
48
49?>
50--EXPECT--
51User information
52----------------
53
54First name:	  Zeev
55Family name:	  Suraski
56Address:	  Ben Gourion 3, Kiryat Bialik, Israel
57Phone:		  +972-4-8713139
58
59
60User information
61----------------
62
63First name:	  Andi
64Family name:	  Gutmans
65Address:	  Haifa, Israel
66Phone:		  +972-4-8231621
67
68
69User information
70----------------
71
72First name:	  Andi
73Family name:	  Gutmans
74Address:	  Haifa, Israel
75Phone:		  +972-4-8231621
76
77
78User information
79----------------
80
81First name:	  Andi
82Family name:	  Gutmans
83Address:	  New address...
84Phone:		  +972-4-8231621
85