I am sure that by now you must have heard that Drupal 8 is using Symfony components and is based on object-oriented programming in PHP. If you are a Drupal 7 developer, then you may not know what is object-oriented programming or fail to understand the benefits it offers. In this post, you will learn the basics of object-oriented PHP programming so that you can start developing for Drupal 8.
What is Object Oriented Programming?
The central concept in the undestanding of Object Oriented Programming is the concept of Object. An Object is a group of data (called properties) and functions (called methods) that go together. Let’s consider a real-life example. A vehicle is an object. It can be defined to have three properties: color, miles driven and warranty (number of miles). It can be defined to have one method: getWarrantyLeft(). This method returns the number of miles of warranty left on the vehicle. Let’s create a template for such a Vehicle object. Open a file Vehicle.php and write the following:
<?php /** * Vehicle class. */ class Vehicle { // Color of the vehicle. var $color = 'red' // Miles driven. var $milesDriven = 0; // Warranty available. var $warranty = 100000; /** * Returns miles of warranty left on the vehicle. * * @return int * Miles of warrnty left on the vehicle. */ function getWarrantyLeft() { if ($this->warranty - $this->milesDriven > 0) { return ($this->warranty - $this->milesDriven); } return 0; } } ?>
Ignore the declaration $this in the code above for now. We’ll explain it later. What we have defined above is a template for a vehicle. Such a template is called Class. It’s not an object. The difference between Class and Object is similar to the difference between a person and you. Person is a common noun and there are billions of people roaming around in the world. On the other hand, there is only one of you. You have all the features of a person, but you are unique from every other person out there. If God were real and were to create another person, he’ll use the same Person template and create one more. That new person will still be different from you. In the same way, there will be multiple objects created from a class template. Your car and my truck, both are vehicles but they are different from each other. To create (or instantiate) an object from a class, you use new operator.
$yourCar = new Vehicle(); $myTruck = new Vehicle();
If you compare the variables $yourCar and $myTruck, you will get FALSE. Notice the operator ===. If you use == operator, you will see TRUE since == checks whether both the objects belong to the same class (which they do), but === checks whether the objects are identical or not.
// This will return FALSE. echo ($yourCar === $myTruck);
Now let’s check what the method getWarrantyLeft() returns. When you are writing procedural code, you can write the following code at the end of Vehicle.php and it will work:
echo getWarrantyLeft();
Now run this file using the command: php Vehicle.php
You will see the output:
$ php Vehicle.php Warranty left on your car: 100000 Warranty left on my truck: 100000
You are seeing the value 100000 because we have defined $warranty to be 100000 in the declaration on line 15 of Vehicle.php and $milesDriven to be 0 on line 12. As a result, 100000 miles of warranty is still left on both the vehicles.
View More 1001
View More 1002
View More 1003
View More 1004
View More 1005
View More 1006
View More 1007
View More 1008
View More 1009
View More 1010
View More 1011
View More 1012
View More 1013
View More 1014
View More 1015
View More 1016
View More 1017
View More 1018
View More 1019
View More 1020
View More 1021
View More 1022
View More 1023
View More 1024
View More 1025
View More 1026
View More 1027
View More 1028
View More 1029
View More 1030
View More 1031
View More 1032
View More 1033
View More 1034
View More 1035
View More 1036
View More 1037
View More 1038
View More 1039
View More 1040
View More 1041
View More 1042
View More 1043
View More 1044
View More 1045
View More 1046
View More 1047
View More 1048
View More 1049
View More 1050
View More 1051
View More 1052
View More 1053
View More 1054
View More 1055
View More 1056
View More 1057
View More 1058
View More 1059
View More 1060
View More 1061
View More 1062
View More 1063
View More 1064
View More 1065
View More 1066
View More 1067
View More 1068
View More 1069
View More 1070
View More 1071
View More 1072
View More 1073
View More 1074
View More 1075
View More 1076
View More 1077
View More 1078
View More 1079
View More 1080
View More 1081
View More 1082
View More 1083
View More 1084
View More 1085
View More 1086
View More 1087
View More 1088
View More 1089
View More 1090
View More 1091
View More 1092
View More 1093
View More 1094
View More 1095
View More 1096
View More 1097
View More 1098
View More 1099
View More 1100
View More 1101
View More 1102
View More 1103
View More 1104
View More 1105
View More 1106
View More 1107
View More 1108
View More 1109
View More 1110
View More 1111
View More 1112
View More 1113
View More 1114
View More 1115
View More 1116
View More 1117
View More 1118
View More 1119
View More 1120
View More 1121
View More 1122
View More 1123
View More 1124
View More 1125
View More 1126
View More 1127
View More 1128
View More 1129
View More 1130
View More 1131
View More 1132
View More 1133
View More 1134
View More 1135
View More 1136
View More 1137
View More 1138
View More 1139
View More 1140
View More 1141
View More 1142
View More 1143
View More 1144
View More 1145
View More 1146
View More 1147
View More 1148
View More 1149
View More 1150
View More 1151
View More 1152
View More 1153
View More 1154
View More 1155
View More 1156
View More 1157
View More 1158
View More 1159
View More 1160
View More 1161
View More 1162
View More 1163
View More 1164
View More 1165
View More 1166
View More 1167
View More 1168
View More 1169
View More 1170
View More 1171
View More 1172
View More 1173
View More 1174
View More 1175
View More 1176
View More 1177
View More 1178
View More 1179
View More 1180
View More 1181
View More 1182
View More 1183
View More 1184
View More 1185
View More 1186
View More 1187
View More 1188
View More 1189
View More 1190
View More 1191
View More 1192
View More 1193
View More 1194
View More 1195
View More 1196
View More 1197
View More 1198
View More 1199
View More 1200
View More 1201
View More 1202
View More 1203
View More 1204
View More 1205
View More 1206
View More 1207
View More 1208
View More 1209
View More 1210
View More 1211
View More 1212
View More 1213
View More 1214
View More 1215
View More 1216
View More 1217
View More 1218
View More 1219
View More 1220
View More 1221
View More 1222
View More 1223
View More 1224
View More 1225
View More 1226
View More 1227
View More 1228
View More 1229
View More 1230
View More 1231
View More 1232
View More 1233
View More 1234
View More 1235
View More 1236
View More 1237
View More 1238
View More 1239
View More 1240
View More 1241
View More 1242
View More 1243
View More 1244
View More 1245
View More 1246
View More 1247
View More 1248
View More 1249
View More 1250
View More 1251
View More 1252
View More 1253
View More 1254
View More 1255
View More 1256
View More 1257
View More 1258
View More 1259
View More 1260
View More 1261
View More 1262
View More 1263
View More 1264
View More 1265
View More 1266
View More 1267
View More 1268
View More 1269
View More 1270
View More 1271
View More 1272
View More 1273
View More 1274
View More 1275
View More 1276
View More 1277
View More 1278
View More 1279
View More 1280
View More 1281
View More 1282
View More 1283
View More 1284
View More 1285
View More 1286
View More 1287
View More 1288
View More 1289
View More 1290
View More 1291
View More 1292
View More 1293
View More 1294
View More 1295
View More 1296
View More 1297
View More 1298
View More 1299
View More 1300
View More 1301
View More 1302
View More 1303
View More 1304
View More 1305
View More 1306
View More 1307
View More 1308
View More 1309
View More 1310
View More 1311
View More 1312
View More 1313
View More 1314
View More 1315
View More 1316
View More 1317
View More 1318
View More 1319
View More 1320
View More 1321
View More 1322
View More 1323
View More 1324
View More 1325
View More 1326
View More 1327
View More 1328
View More 1329
View More 1330
View More 1331
View More 1332
View More 1333
View More 1334
View More 1335
View More 1336
View More 1337
View More 1338
View More 1339
View More 1340
View More 1341
View More 1342
View More 1343
View More 1344
View More 1345
View More 1346
View More 1347
View More 1348
View More 1349
View More 1350
View More 1351
View More 1352
View More 1353
View More 1354
View More 1355
View More 1356
View More 1357
View More 1358
View More 1359
View More 1360
View More 1361
View More 1362
View More 1363
View More 1364
View More 1365
View More 1366
View More 1367
View More 1368
View More 1369
View More 1370
View More 1371
View More 1372
View More 1373
View More 1374
View More 1375
View More 1376
View More 1377
View More 1378
View More 1379
View More 1380
View More 1381
View More 1382
View More 1383
View More 1384
View More 1385
View More 1386
View More 1387
View More 1388
View More 1389
View More 1390
View More 1391
View More 1392
View More 1393
View More 1394
View More 1395
View More 1396
View More 1397
View More 1398
View More 1399
View More 1400
View More 1401
View More 1402
View More 1403
View More 1404
View More 1405
View More 1406
View More 1407
View More 1408
View More 1409
View More 1410
View More 1411
View More 1412
View More 1413
View More 1414
View More 1415
View More 1416
View More 1417
View More 1418
View More 1419
View More 1420
View More 1421
View More 1422
View More 1423
View More 1424
View More 1425
View More 1426
View More 1427
View More 1428
View More 1429
View More 1430
View More 1431
View More 1432
View More 1433
View More 1434
View More 1435
View More 1436
View More 1437
View More 1438
View More 1439
View More 1440
View More 1441
View More 1442
View More 1443
View More 1444
View More 1445
View More 1446
View More 1447
View More 1448
View More 1449
View More 1450
View More 1451
View More 1452
View More 1453
View More 1454
View More 1455
View More 1456
View More 1457
View More 1458
View More 1459
View More 1460
View More 1461
View More 1462
View More 1463
View More 1464
View More 1465
View More 1466
View More 1467
View More 1468
View More 1469
View More 1470
View More 1471
View More 1472
View More 1473
View More 1474
View More 1475
View More 1476
View More 1477
View More 1478
View More 1479
View More 1480
View More 1481
View More 1482
View More 1483
View More 1484
View More 1485
View More 1486
View More 1487
View More 1488
View More 1489
View More 1490
View More 1491
View More 1492
View More 1493
View More 1494
View More 1495
View More 1496
View More 1497
View More 1498
View More 1499
View More 1500