Prev | Current Page 238 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Once declared, each
CHAPTER 6 ?–  O BJECT-ORIENTED PHP 169
field can be used under the terms accorded to it by the scope descriptor. If you don??™t
know what role scope plays in class fields, don??™t worry, that topic is covered later in
this chapter.
Invoking Fields
Fields are referred to using the -> operator and, unlike variables, are not prefaced
with a dollar sign. Furthermore, because a field??™s value typically is specific to a given
object, it is correlated to that object like this:
$object->field
For example, the Employee class includes the fields name, title, and wage. If you
create an object named $employee of type Employee, you would refer to these fields
like this:
$employee->name
$employee->title
$employee->wage
When you refer to a field from within the class in which it is defined, it is still prefaced
with the -> operator, although instead of correlating it to the class name, you use
the $this keyword. $this implies that you??™re referring to the field residing in the same
class in which the field is being accessed or manipulated. Therefore, if you were to
create a method for setting the name field in the Employee class, it might look like this:
function setName($name)
{
$this->name = $name;
}
Field Scopes
PHP supports five class field scopes: public, private, protected, final, and static. The
first four are introduced in this section, and the static scope is introduced in the later
section, ???Static Class Members.


Pages:
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250