Prev | Current Page 236 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


PHP??™s generalized class creation syntax follows:
CHAPTER 6 ?–  O BJECT-ORIENTED PHP 167
class Class_Name
{
// Field declarations defined here
// Method declarations defined here
}
Listing 6-1 depicts a class representing employees.
Listing 6-1. Class Creation
class Employee
{
private $name;
private $title;
protected $wage;
protected function clockIn() {
echo "Member $this->name clocked in at ".date("h:i:s");
}
protected function clockOut() {
echo "Member $this->name clocked out at ".date("h:i:s");
}
}
Titled Employee, this class defines three fields, name, title, and wage, in addition to
two methods, clockIn and clockOut. Don??™t worry if you??™re not familiar with some of
the grammar and syntax; it will become clear later in the chapter.
?– Note While no official PHP code conventions exist, consider following the PHP Extension and Application
Repository guidelines when creating your classes. You can learn more about these conventions at
http://pear.php.net/. These conventions are used throughout the book.
Objects
A class provides a basis from which you can create specific instances of the entity
the class models, better known as objects. For example, an employee management
168 CHAPTER 6 ?–  O BJECT-ORIENTED PHP
application may include an Employee class. You can then call upon this class to create
and maintain specific instances, Sally and Jim, for example.


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