PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 6 ■ OBJECTS AND DESIGN


Let’s take a close look at the attribute in the example. The initial symbol represents the level of
visibility, or access control, for the attribute. Table 6–1 shows the three symbols available.


Table 6–1. Visibility Symbols


Symbol Visibility Explanation

+ Public Available to all code


  • Private Available to the current class only


# Protected Available to the current class and its subclasses only

The visibility symbol is followed by the name of the attribute. In this case, I am describing the
ShopProduct::$price property. A colon is used to separate the attribute name from its type (and
optionally its default value).
Once again, you need only include as much detail as is necessary for clarity.


Operations


Operations describe methods, or more properly, they describe the calls that can be made on an instance
of a class. Figure 6–6 shows two operations in the ShopProduct class.


Figure 6–6. Operations


As you can see, operations use a similar syntax to that used by attributes. The visibility symbol
precedes the method name. A list of parameters is enclosed in parentheses. The method’s return type, if
any, is delineated by a colon. Parameters are separated by commas, and follow the attribute syntax, with
the attribute name separated from its type by a colon.
As you might expect, this syntax is relatively flexible. You can omit the visibility flag and the return
type. Parameters are often represented by their type alone, as the argument name is not usually
significant.


Describing Inheritance and Implementation


The UML describes the inheritance relationship as generalization. This relationship is signified by a line
leading from the subclass to its parent. The line is tipped with an empty closed arrow.
Figure 6–7 shows the relationship between the ShopProduct class and its child classes.

Free download pdf