Prev | Current Page 245 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

14159265';
const E = '2.7182818284';
const EULER = '0.5772156649';
// define other constants and methods here...
}
CHAPTER 6 ?–  O BJECT-ORIENTED PHP 177
Class constants can then be called like this:
echo math_functions::PI;
Methods
A method is quite similar to a function, except that it is intended to define the behavior of
a particular class. Like a function, a method can accept arguments as input and can
return a value to the caller. Methods are also invoked like functions, except that the
method is prefaced with the name of the object invoking the method, like this:
$object->method_name();
In this section you??™ll learn all about methods, including method declaration, method
invocation, and scope.
Declaring Methods
Methods are created in exactly the same fashion as functions, using identical syntax. The
only difference between methods and normal functions is that the method declaration is
typically prefaced with a scope descriptor. The generalized syntax follows:
scope function functionName()
{
// Function body goes here
}
For example, a public method titled calculateSalary() might look like this:
public function calculateSalary()
{
return $this->wage * $this->hours;
}
In this example, the method is directly invoking two class fields, wage and hours,
using the $this keyword. It calculates a salary by multiplying the two field values
together and returns the result just like a function might.


Pages:
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257