forked from intel/php_pgo_training_scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.php
229 lines (197 loc) · 6.29 KB
/
class.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**************************************************************************
* Pgo Train Benchmark
*
* Copyright (c) 2016, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
***************************************************************************/
require_once('constants.php');
/**
* This php file constains classes that simulates WP classes.
*/
class Student {
var $valid = false;
var $string_var = 'This is Student class';
static $num_instances = 0;
var $num_var = 0;
var $float_var = 3.1415;
var $grades = array();
var $grades_string_array = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
var $float_array = array(1.1, 2.2, 3.3, 3.14, 2.73);
var $bool_array = array(true, false, true, true, false);
var $storage = array();
var $end = false;
protected $name;
protected $date;
protected $annual_grade;
private $faculty;
private $university;
public $available_methods = array("set_float", "get_float", "set_personal_info",
"set_fac", "get_fac", "print", "set_grades",
"get_grades", "get_grade", "make_unusable", "to_string");
function Student() {
$this->valid = true;
$this->annual_grade = 0.0;
Student::$num_instances += 1;
}
function set_float($var) {
$this->float_var = $var;
}
function get_float() {
return $this->float_var;
}
function set_personal_info($n, $d, $g) {
$this->name = $n;
$this->date = $d;
$this->annual_grade = $g;
}
function get_personal_info() {
$rez = (string)$this->name . " " . (string)$this->date . " " . (string) $this->annual_grade;
return $rez;
}
function set_fac($f, $u) {
$this->faculty = $f;
$this->university = $u;
}
function get_fac() {
$rez = (string)$this->faculty . " " . (string)$this->university;
return $rez;
}
function print() {
echo Student::$num_instances;
}
function set_grades($g_array) {
$this->grades = $g_array;
$len = sizeof($this->grades);
for ($i=0; $i<$len; $i++) {
$this->annual_grade += $this->grades[$i];
}
$this->annual_grade /= $len;
}
function get_grades() {
return $this->grades;
}
function get_grade() {
return $this->annual_grade;
}
function make_unusable() {
$this->valid = false;
}
function to_string() {
$rez = "Personal info: " . $this->get_personal_info() . "\n\t" . $this->get_fac();
$rez .= "\nGrades: \n";
$grades_array = $this->get_grades();
$len = sizeof($grades_array);
for ($i =0; $i < $len ; $i++) {
$rez .= "\t" . $grades_array[$i] . "\n";
}
$rez .="Annual grade: " . $this->annual_grade . "\n";
return $rez;
}
function get_available_methods() {
return $this->available_methods;
}
}
class LastYearStudent extends Student {
var $group;
function set_group($g) {
$this->group = $g;
}
}
class Faculty {
var $name;
var $city;
var $country;
var $university_name;
var $students = array();
function Faculty($n, $cty, $ctry, $uname) {
$this->name = $n;
$this->city = $cty;
$this->country = $ctry;
$this->university_name = $uname;
}
function add_student($stud) {
array_push($this->students, $stud);
}
}
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/**
* Students are created here and Student class methods
* are called.
*/
function create_student() {
$stud = new Student();
/* some method call by name */
$name = "James J Jordan";
$date = "10-09-1993";
$grade = 0.0;
call_user_func(array($stud, "set_personal_info"), $name, $date, $grade);
$fname = "Automatics and Computer Science";
$uname = "POLITEHNICA University of Bucharest";
call_user_func(array($stud, "set_fac"), $fname, $uname);
/* some clasic method call */
$grades = array(10,9,7,9);
$stud->set_grades($grades);
$description = $stud->to_string();
$methods = $stud->get_available_methods();
for ($i = 0; $i < sizeof($methods); $i++) {
/* call all student getters */
if (startsWith($methods[$i], "get")) {
$rval = call_user_func(array($stud, $methods[$i]));
}
}
return $stud;
}
function create_last_year_student() {
$stud = new LastYearStudent();
/* some method call by name */
$name = "Smithy Jones";
$date = "10-09-1990";
$grade = 0.0;
call_user_func(array($stud, "set_personal_info"), $name, $date, $grade);
$fname = "Automatics and Computer Science";
$uname = "POLITEHNICA University of Bucharest";
call_user_func(array($stud, "set_fac"), $fname, $uname);
/* some clasic method call */
$grades = array(10,9, 10,9);
$stud->set_grades($grades);
$description = $stud->to_string();
$methods = $stud->get_available_methods();
for ($i = 0; $i < sizeof($methods); $i++) {
/* call all student getters */
if (startsWith($methods[$i], "get")) {
$rval = call_user_func(array($stud, $methods[$i]));
}
}
return $stud;
}
/**
* This function creates CLASS_STUDENT_IT students and
* adds them to faculty class
*/
function run_create_classes() {
$fac = new Faculty("Automatics and Computer Science", "Bucharest", "Romania", "POLITEHNICA");
for($i=0; $i < CLASS_STUDENT_IT; $i++) {
$s = create_student();
$fac->add_student($s);
}
$s = create_last_year_student();
$fac->add_student($s);
$s = create_last_year_student();
$fac->add_student($s);
$s = create_last_year_student();
$fac->add_student($s);
}
?>