Repository navigation
Expand file tree
/
Copy pathclasses.php
More file actions
112 lines (89 loc) · 2.4 KB
/
Copy pathclasses.php
File metadata and controls
112 lines (89 loc) · 2.4 KB
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
<?php
// classes with function construct
class Product {
public string $name;
public int $price;
public function __construct($name, $price){
$this->name=$name;
$this->price=$price;
}
}
$product = new Product('Lenovo', 50000);
var_dump($product);
// without using function construct, declared variables will be
?>
<?php
// classes without function construct but already declared the variables' values
class Product2 {
public string $name2 = 'Lenovo';
public int $price2 = 1000;
// public function __construct($name){
// $this->name=$name;
// }
}
$product2 = new Product2();
var_dump($product2);
/* This also works without a constructor because the properties
already have default values.
We don't need to pass arguments when creating the object
because the initial values are already defined in the class. */
?>
<?php
/* there is also a better way of defining properties (inside the constructor) */
class Product3{
public function __construct(
public string $name,
public int $price,
) {
}
public function hasDiscount (): bool{
return $this->price < 22500;
}
}
$product3 = new Product3('Laptop', 25000);
var_dump($product3->hasDiscount());
echo $product3->name;
?>
<?php
class WebApp {
public function __construct(
public string $title,
public int $price,
) {
}
public function isExpensive(): bool {
return $this->price > 50000;
}
public function getDescription(): string {
return "{$this->title} costs {$this->price}";
}
}
$ship = new WebApp ('SaaS Laravel Application', 100000);
var_dump($ship);
var_dump($ship->isExpensive());
var_dump($ship->getDescription());
?>
<?php
// with extension using extends (called extended classes)
class WebApp2 {
public function __construct(
public string $title2,
public int $price2,
) {
}
public function isExpensive2(): bool {
return $this->price2 > 50000;
}
public function getDescription2(): string {
return "{$this->title2} costs {$this->price2}";
}
}
class WebApp3 extends WebApp2 {
public function getLink(): string {
return 'get-Link';
}
}
$ship2 = new WebApp3 ('SaaS Laravel Application', 100000);
var_dump($ship2);
var_dump($ship2->getDescription2());
?>