3 Using PHP Classes Effectively 40: Licensed To Kerrie Firth

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

2.

3 Object references in PHP 4 and PHP 5 31


How object references work 32 ✦ The advantages of object
references 33 ✦ When references are not so useful 33
2.4 Intercepting method calls and class instantiation 34
What is “method overloading”? 34 ✦ Java-style method
overloading in PHP 35 ✦ A near aspect-oriented experience:
logging method calls 36 ✦ Autoloading classes 38
2.5 Summary 39

3 Using PHP classes effectively 40


3.1 Visibility: private and protected methods and variables 41
How visible do we want our methods to be? 42 ✦ When to use
private methods 43 ✦ When to use protected methods 44
Keeping your instance variables private or protected 44
Accessors for private and protected variables 45 ✦ The best of
both worlds? Using interception to control variables 46
Final classes and methods 48
3.2 The class without objects: class methods, variables, and constants 49
Class (static) methods 50 ✦ When to use class methods 51
Class variables 52 ✦ Class constants 53
The limitations of constants in PHP 54
3.3 Abstract classes and methods (functions) 56
What are abstract classes and methods? 56
Using abstract classes 56
3.4 Class type hints 57
How type hints work 58 ✦ When to use type hints 58
3.5 Interfaces 60
What is an interface? 60 ✦ Do we need interfaces in PHP? 61
Using interfaces to make design clearer 61 ✦ Using interfaces to
improve class type hints 62 ✦ Interfaces in PHP 5 versus Java 64
3.6 Summary 64

4 Understanding objects and classes 65


4.1 Why objects and classes are a good idea 66
Classes help you organize 67 ✦ You can tell objects to do
things 67 ✦ Polymorphism 67 ✦ Objects make code easier to
read 68 ✦ Classes help eliminate duplication 73 ✦ You can
reuse objects and classes 74 ✦ Change things without affecting
everything 75 ✦ Objects provide type safety 75
4.2 Criteria for good design 76
Don’t confuse the end with the means 78 ✦ Transparency 78
Simple design 79 ✦ Once and only once 80

viii CONTENTS

Licensed to kerrie firth <k.firth@niwa.co.nz>

You might also like