Slide 1

Slide 1 text

School of PHP Magic Alexander Lisachenko

Slide 2

Slide 2 text

‣ Head of Web Development at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old ‣ Clean code advocate, guru in enterprise architecture ‣ Author of the aspect-oriented framework Go! AOP 
 http://go.aopphp.com About me: 2 lisachenko lisachenko

Slide 3

Slide 3 text

Moscow PHP User Group 3

Slide 4

Slide 4 text

Moscow PHP User Group 3

Slide 5

Slide 5 text

4 «The closer you think you are, the less you'll actually see.»

Slide 6

Slide 6 text

The Trick 5

Slide 7

Slide 7 text

The Trick 5

Slide 8

Slide 8 text

The Trick 6 …an action that is intended to deceive, either as a way of cheating someone, or as a joke or form of entertainment

Slide 9

Slide 9 text

7 One of the trickiest programming language.

Slide 10

Slide 10 text

8 Magic equipment

Slide 11

Slide 11 text

9 Magic equipment

Slide 12

Slide 12 text

9 Magic equipment __construct(), __destruct(), __clone(),

Slide 13

Slide 13 text

9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(),

Slide 14

Slide 14 text

9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(),

Slide 15

Slide 15 text

9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(),

Slide 16

Slide 16 text

9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(),

Slide 17

Slide 17 text

9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __debugInfo()

Slide 18

Slide 18 text

10 Magic equipment

Slide 19

Slide 19 text

10 Magic equipment declare(ticks=1)

Slide 20

Slide 20 text

10 Magic equipment declare(ticks=1) debug_backtrace()

Slide 21

Slide 21 text

10 Magic equipment declare(ticks=1) debug_backtrace() unset(), isset()

Slide 22

Slide 22 text

10 Magic equipment declare(ticks=1) debug_backtrace() unset(), isset() by-reference passing

Slide 23

Slide 23 text

10 Magic equipment declare(ticks=1) debug_backtrace() unset(), isset() by-reference passing bound closures

Slide 24

Slide 24 text

10 Magic equipment declare(ticks=1) debug_backtrace() unset(), isset() by-reference passing bound closures Reflection API

Slide 25

Slide 25 text

10 Magic equipment declare(ticks=1) debug_backtrace() unset(), isset() by-reference passing bound closures Reflection API StreamWrapper API

Slide 26

Slide 26 text

11 So, let’s start our magic!

Slide 27

Slide 27 text

11

Slide 28

Slide 28 text

12 Rule #1 of magic…

Slide 29

Slide 29 text

12 Rule #1 of magic…

Slide 30

Slide 30 text

12

Slide 31

Slide 31 text

13 …always be the smartest guy in the room.

Slide 32

Slide 32 text

13 …always be the smartest guy in the room.

Slide 33

Slide 33 text

13

Slide 34

Slide 34 text

14 Trick #1: Impossible comparison

Slide 35

Slide 35 text

14 Trick #1: Impossible comparison

Slide 36

Slide 36 text

14 Trick #1: Impossible comparison

Slide 37

Slide 37 text

15 Trick #1.1: Not-a-number

Slide 38

Slide 38 text

15 Trick #1.1: Not-a-number

Slide 39

Slide 39 text

16 Trick #1.2: Constant redefinition

Slide 40

Slide 40 text

16 Trick #1.2: Constant redefinition

Slide 41

Slide 41 text

17 Trick #1.3: Tick handler

Slide 42

Slide 42 text

17 Trick #1.3: Tick handler

Slide 43

Slide 43 text

18 Trick #2.1: Magic expressions

Slide 44

Slide 44 text

18 Trick #2.1: Magic expressions

Slide 45

Slide 45 text

18 Trick #2.1: Magic expressions

Slide 46

Slide 46 text

19 True?

Slide 47

Slide 47 text

20 False?

Slide 48

Slide 48 text

21

Slide 49

Slide 49 text

22 Trick #2.1: Operator precedence

Slide 50

Slide 50 text

22 Trick #2.1: Operator precedence Top priority

Slide 51

Slide 51 text

23 Trick #2.2: Magic expressions

Slide 52

Slide 52 text

23 Trick #2.2: Magic expressions

Slide 53

Slide 53 text

23 Trick #2.2: Magic expressions

Slide 54

Slide 54 text

23 Trick #2.2: Magic expressions

Slide 55

Slide 55 text

24 Trick #2.2: Magic expressions

Slide 56

Slide 56 text

24 Trick #2.2: Magic expressions 1) $factory will be used as a class name for «new»

Slide 57

Slide 57 text

24 Trick #2.2: Magic expressions 1) $factory will be used as a class name for «new» 2) Parse error will be thrown

Slide 58

Slide 58 text

24 Trick #2.2: Magic expressions 1) $factory will be used as a class name for «new» 2) Parse error will be thrown 3) Result of method $factory->build() will be used

Slide 59

Slide 59 text

24 Trick #2.2: Magic expressions 1) $factory will be used as a class name for «new» 2) Parse error will be thrown 3) Result of method $factory->build() will be used 4) Value of property $factory->build will be used

Slide 60

Slide 60 text

25 Trick #2.2: Magic expressions

Slide 61

Slide 61 text

25 Trick #2.2: Magic expressions

Slide 62

Slide 62 text

26 Trick #2.2: Operator precedence

Slide 63

Slide 63 text

26 Trick #2.2: Operator precedence

Slide 64

Slide 64 text

27 Trick #2.3: Magic expressions

Slide 65

Slide 65 text

27 Trick #2.3: Magic expressions

Slide 66

Slide 66 text

27 Trick #2.3: Magic expressions

Slide 67

Slide 67 text

28 Trick #2.3: Loophole in parser

Slide 68

Slide 68 text

29

Slide 69

Slide 69 text

29 What is magic?

Slide 70

Slide 70 text

29 What is magic? Focused deception.

Slide 71

Slide 71 text

29 What is magic? Focused deception. But deception meant to entertain.

Slide 72

Slide 72 text

29 What is magic? Focused deception. But deception meant to entertain. Try to remember next one card…

Slide 73

Slide 73 text

29

Slide 74

Slide 74 text

30

Slide 75

Slide 75 text

30

Slide 76

Slide 76 text

31 I am bad magician :) But let me try…

Slide 77

Slide 77 text

31 I am bad magician :) But let me try…

Slide 78

Slide 78 text

32 Trick #3: Breaking the rules

Slide 79

Slide 79 text

32 Trick #3: Breaking the rules

Slide 80

Slide 80 text

32 Trick #3: Breaking the rules

Slide 81

Slide 81 text

33

Slide 82

Slide 82 text

33 Who knows how to do this?

Slide 83

Slide 83 text

33 Who knows how to do this? Who knows two solutions?

Slide 84

Slide 84 text

33 Who knows how to do this? Who knows two solutions? Who knows three solutions?

Slide 85

Slide 85 text

33 Who knows how to do this? Who knows two solutions? Who knows three solutions? Who knows more than three?

Slide 86

Slide 86 text

34 Trick #3.1: Reflection way

Slide 87

Slide 87 text

34 Trick #3.1: Reflection way

Slide 88

Slide 88 text

Trick #3.2: Bound closure 35

Slide 89

Slide 89 text

Trick #3.2: Bound closure 35

Slide 90

Slide 90 text

Trick #3.2: Bound closure 35

Slide 91

Slide 91 text

Trick #3.3: Unserialisation trick 36

Slide 92

Slide 92 text

Trick #3.3: Unserialisation trick 36

Slide 93

Slide 93 text

Trick #3.3: Unserialisation trick 36

Slide 94

Slide 94 text

Trick #3.3: Unserialisation trick 36

Slide 95

Slide 95 text

Trick #3.4: Can you find it? 37

Slide 96

Slide 96 text

Trick #3: doctrine/instantiator package 38 composer show doctrine/instantiator --all name : doctrine/instantiator descrip. : A small, lightweight utility to instantiate objects in PHP without invoking their constructors keywords : constructor, instantiate type : library license : MIT License (MIT)

Slide 97

Slide 97 text

Trick #4: Intercepting property access 39

Slide 98

Slide 98 text

Trick #4: Intercepting property access 39

Slide 99

Slide 99 text

Trick #4: Intercepting property access 39

Slide 100

Slide 100 text

Trick #4: Intercepting property access 39

Slide 101

Slide 101 text

Trick #4.1: Add magic… getter! 40

Slide 102

Slide 102 text

Trick #4.1: Add magic… getter! 40

Slide 103

Slide 103 text

Trick #4.2: Use previous trick! 41

Slide 104

Slide 104 text

Trick #4.2: Use previous trick! 41

Slide 105

Slide 105 text

Trick #4.3: Unset this private field 42

Slide 106

Slide 106 text

Trick #4.3: Unset this private field 42

Slide 107

Slide 107 text

Trick #4.4: Call private constructor 43

Slide 108

Slide 108 text

Trick #4.4: Call private constructor 43

Slide 109

Slide 109 text

Trick #4: Result 44

Slide 110

Slide 110 text

Trick #4: leedavis/altr-ego package 45 composer show leedavis81/altr-ego --all name : leedavis81/altr-ego descrip. : Access an objects protected / private properties and methods keywords : php, break scope versions : dev-master, v1.0.2, v1.0.1, v1.0.0 type : library license : MIT License (MIT)

Slide 111

Slide 111 text

Trick #5: Immutable objects in PHP? 46

Slide 112

Slide 112 text

Trick #5: Immutable objects in PHP? 46

Slide 113

Slide 113 text

Trick #5: Immutable objects in PHP? 46

Slide 114

Slide 114 text

47 True?

Slide 115

Slide 115 text

48 False?

Slide 116

Slide 116 text

49 Trick #5.1: Immutable object in PHP

Slide 117

Slide 117 text

50 Trick #5.1: Immutable object in PHP

Slide 118

Slide 118 text

50 Trick #5.1: Immutable object in PHP Unfortunately, you can’t even read it!

Slide 119

Slide 119 text

51 Trick #5.2: Immutable object in PHP

Slide 120

Slide 120 text

51 Trick #5.2: Immutable object in PHP

Slide 121

Slide 121 text

52

Slide 122

Slide 122 text

52 What if we use previous tricks?

Slide 123

Slide 123 text

52 What if we use previous tricks? Intercept properties.

Slide 124

Slide 124 text

52 What if we use previous tricks? Intercept properties. Securely store values of properties.

Slide 125

Slide 125 text

52 What if we use previous tricks? Intercept properties. Securely store values of properties. Provide read-only API.

Slide 126

Slide 126 text

52 What if we use previous tricks? Intercept properties. Securely store values of properties. Provide read-only API.

Slide 127

Slide 127 text

52 What if we use previous tricks? Intercept properties. Securely store values of properties. Provide read-only API.

Slide 128

Slide 128 text

52 What if we use previous tricks? Intercept properties. Securely store values of properties. Provide read-only API.

Slide 129

Slide 129 text

53

Slide 130

Slide 130 text

53 Secure place for values:

Slide 131

Slide 131 text

53 Secure place for values: Global variables

Slide 132

Slide 132 text

53 Secure place for values: Global variables Public properties

Slide 133

Slide 133 text

53 Secure place for values: Global variables Public properties Protected properties

Slide 134

Slide 134 text

53 Secure place for values: Global variables Public properties Protected properties Private properties

Slide 135

Slide 135 text

53 Secure place for values: Global variables Public properties Protected properties Private properties Private static properties

Slide 136

Slide 136 text

53 Secure place for values: Global variables Public properties Protected properties Private properties Private static properties Static variables in functions

Slide 137

Slide 137 text

53 Secure place for values: Global variables Public properties Protected properties Private properties Private static properties Static variables in functions

Slide 138

Slide 138 text

54 Trick #5.2: Secure value storage

Slide 139

Slide 139 text

54 Trick #5.2: Secure value storage

Slide 140

Slide 140 text

55 Trick #5.2: Use it by-reference

Slide 141

Slide 141 text

55 Trick #5.2: Use it by-reference

Slide 142

Slide 142 text

56 Trick #5.2: Set state implementation

Slide 143

Slide 143 text

56 Trick #5.2: Set state implementation

Slide 144

Slide 144 text

Trick #5.2: Applying object state 57

Slide 145

Slide 145 text

Trick #5.2: Get state implementation 58

Slide 146

Slide 146 text

Trick #5.2: Get state implementation

Slide 147

Slide 147 text

Trick #5.2: And a little bit magic :) 60

Slide 148

Slide 148 text

Trick #5.2: And a little bit magic :) 60

Slide 149

Slide 149 text

Trick #5: lisachenko/immutable-object 61 composer show lisachenko/immutable-object --all name : lisachenko/immutable-object descrip. : Immutable object library keywords : versions : * dev-master type : library license : MIT License (MIT)

Slide 150

Slide 150 text

Trick #5: Usage example 62

Slide 151

Slide 151 text

Trick #5: Usage example 63

Slide 152

Slide 152 text

Trick #5: Try to change it… 64

Slide 153

Slide 153 text

Trick #5: Try to change it… 64

Slide 154

Slide 154 text

Trick #5: Try to change it… 65

Slide 155

Slide 155 text

Trick #5: Try to change it… 65 Maybe you can hack it?

Slide 156

Slide 156 text

Trick #6: It contains anti-XDebug code! 66

Slide 157

Slide 157 text

Trick #7: Stream processing

Slide 158

Slide 158 text

Trick #7: Stream processing

Slide 159

Slide 159 text

Trick #7: Stream processing

Slide 160

Slide 160 text

Trick #7: Stream processing

Slide 161

Slide 161 text

Trick #7: Stream processing

Slide 162

Slide 162 text

Trick #7: Stream processing

Slide 163

Slide 163 text

Trick #8: Aspect-oriented programming

Slide 164

Slide 164 text

Trick #8: Aspect-oriented programming

Slide 165

Slide 165 text

Is this code good or bad? 72

Slide 166

Slide 166 text

Is this code good or bad? 72

Slide 167

Slide 167 text

Is this code good or bad? 72

Slide 168

Slide 168 text

Is this code good or bad? 72

Slide 169

Slide 169 text

Same code without secondary concerns 73

Slide 170

Slide 170 text

Same code without secondary concerns 73

Slide 171

Slide 171 text

Authorization control… 74

Slide 172

Slide 172 text

Authorization control… 74

Slide 173

Slide 173 text

Logging and audit… 75

Slide 174

Slide 174 text

Logging and audit… 75

Slide 175

Slide 175 text

Error handling… 76

Slide 176

Slide 176 text

Error handling… 76

Slide 177

Slide 177 text

77

Slide 178

Slide 178 text

77

Slide 179

Slide 179 text

Aspect glossary 78

Slide 180

Slide 180 text

Aspect glossary 78

Slide 181

Slide 181 text

Aspect glossary Pointcut - describes list of interesting events 78

Slide 182

Slide 182 text

Aspect glossary Pointcut - describes list of interesting events 78 Joinpoint - defines an event object

Slide 183

Slide 183 text

Aspect glossary Pointcut - describes list of interesting events Advice - event handler 78 Joinpoint - defines an event object

Slide 184

Slide 184 text

Aspect VS Event Listener 79

Slide 185

Slide 185 text

Aspect VS Event Listener 79

Slide 186

Slide 186 text

Aspect VS Event Listener 79

Slide 187

Slide 187 text

Aspect VS Event Listener 79

Slide 188

Slide 188 text

80 All aspects (or advisors) are registered in the aspect kernel

Slide 189

Slide 189 text

80 All aspects (or advisors) are registered in the aspect kernel

Slide 190

Slide 190 text

81 The special php://filter stream filter is registered via stream_filter_register()

Slide 191

Slide 191 text

82 The composer class loader is replaced with a weaving proxy

Slide 192

Slide 192 text

83 Lexical analysis and parsing of source code into the AST is performed (nikic/PHP-Parser)

Slide 193

Slide 193 text

84 Static reflection is created from the AST (goaop/parser-reflection)

Slide 194

Slide 194 text

84 Static reflection is created from the AST (goaop/parser-reflection)

Slide 195

Slide 195 text

84 Static reflection is created from the AST (goaop/parser-reflection)

Slide 196

Slide 196 text

84 Static reflection is created from the AST (goaop/parser-reflection)

Slide 197

Slide 197 text

85 The original class is renamed and replaced with a new class with additional behavior; stored in the cache

Slide 198

Slide 198 text

85 The original class is renamed and replaced with a new class with additional behavior; stored in the cache

Slide 199

Slide 199 text

85 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Same class name!

Slide 200

Slide 200 text

85 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Original class renamed Same class name!

Slide 201

Slide 201 text

85 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Original class renamed Overridden method Same class name!

Slide 202

Slide 202 text

86 What to expect in future:

Slide 203

Slide 203 text

86 What to expect in future: Opcache preloading for AOP core

Slide 204

Slide 204 text

86 What to expect in future: Opcache preloading for AOP core FFI integration to modify binary opcodes

Slide 205

Slide 205 text

86 What to expect in future: Opcache preloading for AOP core FFI integration to modify binary opcodes Modifying PHP engine internal callbacks

Slide 206

Slide 206 text

86 What to expect in future: Opcache preloading for AOP core FFI integration to modify binary opcodes Modifying PHP engine internal callbacks Some more magic?

Slide 207

Slide 207 text

Trick #8: goaop/framework 87 composer show goaop/framework --all name : goaop/framework descrip. : Framework for aspect-oriented programming in PHP. keywords : php, aop, library, aspect versions : dev-master, 3.0.x-dev, 2.x-dev, 2.3.1, … type : library license : MIT License

Slide 208

Slide 208 text

If it is too magic for you, just use PhpStorm plugin for that! 88

Slide 209

Slide 209 text

If it is too magic for you, just use PhpStorm plugin for that! 88

Slide 210

Slide 210 text

Pointcut syntax highlighting, completion and analysis 89

Slide 211

Slide 211 text

Pointcut syntax highlighting, completion and analysis 89

Slide 212

Slide 212 text

Navigate to advice/advised elements 90

Slide 213

Slide 213 text

Navigate to advice/advised elements 90

Slide 214

Slide 214 text

Текст Trick #9: Deferred methods

Slide 215

Slide 215 text

92 Trick: prevent execution of methods wrapping them into promises and run after the fastcgi_finish_request.

Slide 216

Slide 216 text

Trick 9.1. Define an annotation 93

Slide 217

Slide 217 text

Trick 9.2. Define an aspect 94

Slide 218

Slide 218 text

Trick 9.2. Define an aspect 94

Slide 219

Slide 219 text

Trick 9.2. Define an aspect 94 All methods with «Async» annotation

Slide 220

Slide 220 text

Trick 9.2. Define an aspect 94 Record each call with arguments

Slide 221

Slide 221 text

Trick 9.2. Define an aspect 94 Prevent execution of original method

Slide 222

Slide 222 text

Trick 9.2. Define an aspect 94 Return «our» value

Slide 223

Slide 223 text

Trick 9.2. Define an aspect 95

Slide 224

Slide 224 text

Trick 9.2. Define an aspect 95

Slide 225

Slide 225 text

Trick 9.2. Define an aspect 95

Slide 226

Slide 226 text

Trick 9.2. Define an aspect 95

Slide 227

Slide 227 text

Trick 9.2. Define an aspect 95 Finish FASTCGI Request

Slide 228

Slide 228 text

Trick 9.2. Define an aspect 95 Execute delayed methods

Slide 229

Slide 229 text

Trick 9.3. Let’s use services 96

Slide 230

Slide 230 text

Trick 9.3. Let’s use services 96 Some long service call

Slide 231

Slide 231 text

Trick 9.3. Let’s use services 96

Slide 232

Slide 232 text

Trick 9.4. Declare deferred 97

Slide 233

Slide 233 text

Trick 9.4. Declare deferred 97 Now it will be deferred

Slide 234

Slide 234 text

Trick 9.4. Declare deferred 97

Slide 235

Slide 235 text

98 The End

Slide 236

Slide 236 text

98 The End

Slide 237

Slide 237 text

Thank you! Please, leave your feedback! 99 https://joind.in/event/php-russia-2019/school-of-php-magic---php