Methods i Object-Orienter Programming (OOP) are similiar to functions in PHP.
public | private | protected] function functionName ([parameterList]) { // Statements to execute }
public function getSummary() { $maxLength = 25; $summary = $this->description; if (strlen($summary) > $maxLength) { $summary = substr($summary, 0, $maxLength - 3) . '...'; } return $summary }
private function internationalizePrice($country = 'US') { switch ($country) { case 'US': return '$' . number_format($this->price, 2); case 'FR': return number_format($this->price, 2, ',' , '.') . ' EUR'; default: return number_format($this->price, 2); } }
public function showDescription() { echo $this->description; }
public function showPrice($country = 'US') { echo $this->internationalizePrice($country); }