-
Notifications
You must be signed in to change notification settings - Fork 90
/
02-properties.php
50 lines (38 loc) · 1.46 KB
/
02-properties.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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 02 - Properties */
// classes have properties that contain information about that object defined by the class
class Mobile {
public $name; // Public makes sure we can access this properties outside the class.
public $chipset;
public $internalMemory;
}
$modernMobile = new Mobile();
// in OOP and PHP we use the arrow operator ( -> ) for accessing properties
// we can assign those public properties in our class with this syntax
$modernMobile->name = "Samsung s20";
$modernMobile->chipset = "Exynos";
$modernMobile->internalMemory = 128;
echo "<br>";
// we can also access properties value by the arrow operator
echo $modernMobile->chipset;
// what happens if we assign a non existing value or a wrong value?
$modernMobile->ramMemory = 1;
$modernMobile->internMemory = 8;
echo "<pre>";
var_dump($modernMobile);
echo "</pre>";
echo "<br>";
//-----------------------------------------------------
// arrow operator is unique in objects and must not be
// confused with double arrow operator used in arrays
//-----------------------------------------------------
// this is an example using an array and double arrow operators
$mobileArray = [
'name' => 'Xiaomi Mi10',
'chipset' => 'Snapdragon',
'internalMemory' => 64
];
echo $mobileArray['chipset'];