forked from 20jam/php-oop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraits.php
More file actions
115 lines (97 loc) · 2.73 KB
/
traits.php
File metadata and controls
115 lines (97 loc) · 2.73 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
113
114
115
<?php
// Example 1: -----------------------------------------------------------------
// source:
// https://leanpub.com/the-essentials-of-object-oriented-php
// Context:
// a `Price` trait has a method `changePriceByDollars()` that // calculates the new price
// from the old price and the price change in dollars. and another trait for special prices
// with a method that announce that the model is on special sell:
// Define traits
trait Price {
public function changePriceByDollars($price, $change)
{
return $price + $change;
}
}
trait SpecialSell {
public function annonunceSpecialSell() {
return __CLASS__ . " on special sell";
}
}
// Import/use them in classes:
class Bwm {
use Price;
use SpecialSell;
}
class Mercedes {
use Price;
use SpecialSell;
}
// Use them:
$mercedes1 = new Mercedes();
echo $mercedes1->changePriceByDollars(42000, -2100);
echo $mercedes1->annonunceSpecialSell();
// Example 2: -----------------------------------------------------------------
// source:
// https://www.php.net/manual/en/language.oop5.traits.php
// Context:
// Hello world
trait HelloWorld {
public function sayHello() {
echo 'Hello World!';
}
}
class TheWorldIsNotEnough {
use HelloWorld;
public function sayHello() {
echo 'Hello Universe!';
}
}
(new TheWorldIsNotEnough)->sayHello();
// Example 3: -----------------------------------------------------------------
// source:
// https://www.php.net/manual/en/language.oop5.traits.php
// Context:
// Logger for new account and new user
trait Logger {
function log($msg) {
echo date('Y-m-d h:i:s') . ':' . '(' . __CLASS__ . ') ' . $msg . PHP_EOL;
}
}
class BankAccount{
use Logger;
private $accountNumber;
function __construct($accountNumber){
$this->accountNumber = $accountNumber;
$this->log("A new $accountNumber bank account created");
}
}
class User{
use Logger;
function __construct() {
$this->log("A new user created");
}
}
(new BankAccount('1234567674'));
(new User());
// Example 4: -----------------------------------------------------------------
// source:
// https://www.php.net/manual/en/language.oop5.traits.php
// Context:
// Controllers and CRUD
class Controller {
/* Controller-specific methods defined here. */
}
class AdminController extends Controller {
/* Controller-specific methods inherited from Controller. */
/* Admin-specific methods defined here. */
}
trait CrudControllerTrait {
/* CRUD-specific methods defined here. */
}
class AdminCrudController extends AdminController {
use CrudControllerTrait;
/* Controller-specific methods inherited from Controller. */
/* Admin-specific methods inherited from AdminController. */
/* CRUD-specific methods defined by CrudControllerTrait. */
}