Oop user class
Hallo allemaal ik heb een vraagje ik probeer me zelf een beetje oop aan te leren.
Ik wil graag jullie vragen om te kijken hoe het geschreven is en mijn te vertellen wat hier beter aan kan.
Ik wil graag jullie vragen om te kijken hoe het geschreven is en mijn te vertellen wat hier beter aan kan.
Code (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
230
231
232
233
234
235
236
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
230
231
232
233
234
235
236
<?php
class User_Data
{
private $User_id;
public function __construct($User_id){
$this->User_id = $User_id;
$query = tep_db_query("SELECT a.avatar, s.signature, g.user_id, g.user_name, g.geboortedatum, g.geslacht,g.avatar_default, g.signaturetyp, g.signature_default, g.signaturetext, g.land, DATE_SUB(NOW(),INTERVAL 5 MINUTE) <= g.lastactive AS online, g.status FROM " .TABLE_USERS. " AS g LEFT JOIN " .TABLE_AVATAR. " AS a ON a.bid=g.avatar_default LEFT JOIN " .TABLE_SIGNATURE. " AS s ON s.bid = g.signature_default WHERE g.user_id='".(int)$this->User_id."'");
$res = tep_db_fetch_array($query);
$this->user_id = $res["user_id"];
$this->user_name = $res["user_name"];
$this->birth = $res["geboortedatum"];
$this->gender = $res["geslacht"];
$this->avatar = $res["avatar"];
$this->avatar_default = $res["avatar_default"];
$this->signature = $res["signature"];
$this->signaturetyp = $res["signaturetyp"];
$this->signature_default = $res["signature_default"];
$this->signaturetext = $res["signaturetext"];
$this->land = $res["land"];
$this->online = $res["online"];
$this->status = $res["status"];
}
public function Get_user_id() {
return $this->user_id;
}
public function Get_user_name() {
return $this->user_name;
}
public function Get_birth() {
return $this->birth;
}
public function Get_gender() {
return $this->gender;
}
public function Get_avatar() {
return $this->avatar;
}
public function Get_avatar_default() {
return $this->avatar_default;
}
public function Get_signature() {
return $this->signature;
}
public function Get_signaturetyp() {
return $this->signaturetyp;
}
public function Get_signature_default() {
return $this->signature_default;
}
public function Get_signaturetext() {
return $this->signaturetext;
}
public function Get_land() {
return $this->land;
}
public function Get_online() {
return $this->online;
}
public function Get_status() {
return $this->status;
}
public function Get_user_dir(){
$User_name = strtolower ($this->Get_user_name());
$eerste_letter = substr($User_name, 0, 1);
$tweede_letter = substr($User_name, 1, 1);
$User_dir = 'users/'.$eerste_letter.'/'.$tweede_letter .'/'.$User_name.'/';
return $User_dir;
}
}
class Avatar extends User_Data
{
private $Avatar_default_file = "avatar.gif";
private function Get_avatar_dir(){
$Avatar_dir = $this->Get_user_dir()."avatar_members/";
return $Avatar_dir;
}
private function Set_avatar_default(){
$Avatar_location == HTTP_SERVER."images/".$this->Avatar_default_file();
return $Avatar_location;
}
private function Check_avatar(){
if(!is_file($this->avatar)){
return false;
}
}
private function Set_avatar(){
if($this->avatar_default==0) {
$Avatar=$this->Set_avatar_default;
}else{
if($this->Check_avatar=false){
}else{
$Avatar=HTTP_SERVER.$this->Get_avatar_dir().$this->Get_avatar();
}
}
return $Avatar;
}
private function Set_avatar_img(){
$Avatar = "<img src=\"".$this->Set_avatar()."\" />";
return $Avatar;
}
public function Display(){
$Avatar = $this->Set_avatar_img().$this->avatar;
}
}
class Status extends User_Data
{
private function Check_online(){
if($this->Get_online()==1){
return true;
}
}
public function Get_status_user($Type){
switch($this->Get_status()){
case "1":
$Status_name = T_MOD_COMMENTHANDLER_46;
$Status_icon = "status_online";
break;
case "2":
$Status_name = T_MOD_COMMENTHANDLER_47;
$Status_icon = "status_offline";
break;
case "3":
$Status_name = T_MOD_COMMENTHANDLER_48;
$Status_icon = "status_afwezig";
break;
case "4":
$Status_name = T_MOD_COMMENTHANDLER_49;
$Status_icon = "status_bezet";
break;
}
if($Type==1){
$Status = $Status_name;
}else{
$Status = $Status_icon;
}
return $Status;
}
private function Set_status_offline_user($Type){
$Status_name = T_MOD_COMMENTHANDLER_6;
$Status_icon = "status_offline";
if($Type==1){
$Status = $Status_name;
}else{
$Status = $Status_icon;
}
return $Status;
}
private function Set_status($Type){
if($this->Check_online()==true){
$Status = $this->Get_status_user($Type);
$Status = $this->Get_status_user($Type);
}else{
$Status = $this->Set_status_offline_user($Type);
}
return $Status;
}
public function Display($Type){
if($Type==1){
$Status = "<div class=\"user_status_icon_1\"><b>".$this->Set_status($Type)."</b></div><div class=\"user_status_icon_2\">";
}else{
$Status = "<img src=\"".HTTP_SERVER.DIR_WS_IMAGES."icons/".$this->Set_status($Type).".png\" /></div>";
}
return $Status;
}
}
class Signature extends User_Data
{
private function Get_signature_dir(){
$this->Get_user_dir();
}
private function Set_signature_default(){
$Signature_location = $this->Get_user_dir()."signature_members/".$this->Get_signature();
return $Signature_location;
}
private function Set_signature_img(){
$Signature = "<img src=\"".$this->Set_signature_default()."\"/>";
return $Signature;
}
private function Set_signature_txt(){
$this->Get_signaturetext();
return $this->Get_signaturetext();
}
private function Set_signature(){
if($this->Get_signaturetyp()==1){
$Signature = $this->Set_signature_txt();
}else{
$Signature = $this->Set_signature_img();
}
return $Signature;
}
public function Display(){
return $this->Set_signature();
}
}
?>
class User_Data
{
private $User_id;
public function __construct($User_id){
$this->User_id = $User_id;
$query = tep_db_query("SELECT a.avatar, s.signature, g.user_id, g.user_name, g.geboortedatum, g.geslacht,g.avatar_default, g.signaturetyp, g.signature_default, g.signaturetext, g.land, DATE_SUB(NOW(),INTERVAL 5 MINUTE) <= g.lastactive AS online, g.status FROM " .TABLE_USERS. " AS g LEFT JOIN " .TABLE_AVATAR. " AS a ON a.bid=g.avatar_default LEFT JOIN " .TABLE_SIGNATURE. " AS s ON s.bid = g.signature_default WHERE g.user_id='".(int)$this->User_id."'");
$res = tep_db_fetch_array($query);
$this->user_id = $res["user_id"];
$this->user_name = $res["user_name"];
$this->birth = $res["geboortedatum"];
$this->gender = $res["geslacht"];
$this->avatar = $res["avatar"];
$this->avatar_default = $res["avatar_default"];
$this->signature = $res["signature"];
$this->signaturetyp = $res["signaturetyp"];
$this->signature_default = $res["signature_default"];
$this->signaturetext = $res["signaturetext"];
$this->land = $res["land"];
$this->online = $res["online"];
$this->status = $res["status"];
}
public function Get_user_id() {
return $this->user_id;
}
public function Get_user_name() {
return $this->user_name;
}
public function Get_birth() {
return $this->birth;
}
public function Get_gender() {
return $this->gender;
}
public function Get_avatar() {
return $this->avatar;
}
public function Get_avatar_default() {
return $this->avatar_default;
}
public function Get_signature() {
return $this->signature;
}
public function Get_signaturetyp() {
return $this->signaturetyp;
}
public function Get_signature_default() {
return $this->signature_default;
}
public function Get_signaturetext() {
return $this->signaturetext;
}
public function Get_land() {
return $this->land;
}
public function Get_online() {
return $this->online;
}
public function Get_status() {
return $this->status;
}
public function Get_user_dir(){
$User_name = strtolower ($this->Get_user_name());
$eerste_letter = substr($User_name, 0, 1);
$tweede_letter = substr($User_name, 1, 1);
$User_dir = 'users/'.$eerste_letter.'/'.$tweede_letter .'/'.$User_name.'/';
return $User_dir;
}
}
class Avatar extends User_Data
{
private $Avatar_default_file = "avatar.gif";
private function Get_avatar_dir(){
$Avatar_dir = $this->Get_user_dir()."avatar_members/";
return $Avatar_dir;
}
private function Set_avatar_default(){
$Avatar_location == HTTP_SERVER."images/".$this->Avatar_default_file();
return $Avatar_location;
}
private function Check_avatar(){
if(!is_file($this->avatar)){
return false;
}
}
private function Set_avatar(){
if($this->avatar_default==0) {
$Avatar=$this->Set_avatar_default;
}else{
if($this->Check_avatar=false){
}else{
$Avatar=HTTP_SERVER.$this->Get_avatar_dir().$this->Get_avatar();
}
}
return $Avatar;
}
private function Set_avatar_img(){
$Avatar = "<img src=\"".$this->Set_avatar()."\" />";
return $Avatar;
}
public function Display(){
$Avatar = $this->Set_avatar_img().$this->avatar;
}
}
class Status extends User_Data
{
private function Check_online(){
if($this->Get_online()==1){
return true;
}
}
public function Get_status_user($Type){
switch($this->Get_status()){
case "1":
$Status_name = T_MOD_COMMENTHANDLER_46;
$Status_icon = "status_online";
break;
case "2":
$Status_name = T_MOD_COMMENTHANDLER_47;
$Status_icon = "status_offline";
break;
case "3":
$Status_name = T_MOD_COMMENTHANDLER_48;
$Status_icon = "status_afwezig";
break;
case "4":
$Status_name = T_MOD_COMMENTHANDLER_49;
$Status_icon = "status_bezet";
break;
}
if($Type==1){
$Status = $Status_name;
}else{
$Status = $Status_icon;
}
return $Status;
}
private function Set_status_offline_user($Type){
$Status_name = T_MOD_COMMENTHANDLER_6;
$Status_icon = "status_offline";
if($Type==1){
$Status = $Status_name;
}else{
$Status = $Status_icon;
}
return $Status;
}
private function Set_status($Type){
if($this->Check_online()==true){
$Status = $this->Get_status_user($Type);
$Status = $this->Get_status_user($Type);
}else{
$Status = $this->Set_status_offline_user($Type);
}
return $Status;
}
public function Display($Type){
if($Type==1){
$Status = "<div class=\"user_status_icon_1\"><b>".$this->Set_status($Type)."</b></div><div class=\"user_status_icon_2\">";
}else{
$Status = "<img src=\"".HTTP_SERVER.DIR_WS_IMAGES."icons/".$this->Set_status($Type).".png\" /></div>";
}
return $Status;
}
}
class Signature extends User_Data
{
private function Get_signature_dir(){
$this->Get_user_dir();
}
private function Set_signature_default(){
$Signature_location = $this->Get_user_dir()."signature_members/".$this->Get_signature();
return $Signature_location;
}
private function Set_signature_img(){
$Signature = "<img src=\"".$this->Set_signature_default()."\"/>";
return $Signature;
}
private function Set_signature_txt(){
$this->Get_signaturetext();
return $this->Get_signaturetext();
}
private function Set_signature(){
if($this->Get_signaturetyp()==1){
$Signature = $this->Set_signature_txt();
}else{
$Signature = $this->Set_signature_img();
}
return $Signature;
}
public function Display(){
return $this->Set_signature();
}
}
?>
Gesponsorde koppelingen:
De class User_Data vind ik vreemd, de naam doet een collectie gegevens over een User vermoeden maar in OOP zouden dat eigenschappen van het User object moeten zijn.
Dat je denkwijze niet helemaal klopt, blijkt ook uit de wijze waarop je deze class extend. Een Signature is een eigenschap van een User en dus geen vorm van een User. Je kunt die class dus ook niet op die manier extenden.
Bovendien is je User_Data class niet onafhankelijk. Voordat je deze class kunt instantieren moet je een databaseverbinding hebben omdat anders de constructor de soep in loop. Netter zou zijn om ofwel deze verbinding met de constructor af te dwingen (door als parameter mee te geven) ofwel deze verbinding in de constructor opzetten.
Dat je denkwijze niet helemaal klopt, blijkt ook uit de wijze waarop je deze class extend. Een Signature is een eigenschap van een User en dus geen vorm van een User. Je kunt die class dus ook niet op die manier extenden.
Bovendien is je User_Data class niet onafhankelijk. Voordat je deze class kunt instantieren moet je een databaseverbinding hebben omdat anders de constructor de soep in loop. Netter zou zijn om ofwel deze verbinding met de constructor af te dwingen (door als parameter mee te geven) ofwel deze verbinding in de constructor opzetten.
Bedankt voor de tips, Even kijken of ik het begrijp jij zou bv de query niet in de class plaatse ?
Met extend kun je toch de gegevens van de hooft class op halen zonder deze aan te roepen ? Dus hij neemt toch alle gegevens over bv de naam het id het type Signature waar door je niet weer de class user_data op nieuw op hoeft te halen.
Met extend kun je toch de gegevens van de hooft class op halen zonder deze aan te roepen ? Dus hij neemt toch alle gegevens over bv de naam het id het type Signature waar door je niet weer de class user_data op nieuw op hoeft te halen.
Deze query zou ik inderdaad niet in deze class plaatsen. Maar dat houdt niet in dat je nooit queries in een class kwijt kunt.
Wat betreft het extenden: je kunt class X alleen extenden met class Y als Y ook altijd een X is. En dat is in dit geval zeker niet zo...
Lees ook: OOP beginnershandleiding
Wat betreft het extenden: je kunt class X alleen extenden met class Y als Y ook altijd een X is. En dat is in dit geval zeker niet zo...
Lees ook: OOP beginnershandleiding



