Home arrow PHP arrow Page 3 - Object-Oriented Programming Through Design Patterns

Inheritance - PHP

If you're looking for an overview of the new object-oriented features of PHP 5, you've found a good place to start. This article, the first of several parts, is excerpted from chapter two of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).

TABLE OF CONTENTS:
  1. Object-Oriented Programming Through Design Patterns
  2. Introduction to OO Programming
  3. Inheritance
  4. Static (or Class) Attributes and Methods
By: Sams Publishing
Rating: starstarstarstarstar / 16
September 21, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You use inheritance when you want to create a new class that has properties or behaviors similar to those of an existing class. To provide inheritance, PHP supports the ability for a class to extend an existing class. When you extend a class, the new class inherits all the properties and methods of the parent (with a couple exceptions, as described later in this chapter). You can both add new methods and properties and override the exiting ones. An inheritance relationship is defined with the word extends. Let's extend User to make a new class representing users with administrative privileges. We will augment the class by selecting the user's password from an NDBM file and providing a comparison function to compare the user's password with the password the user supplies:

class AdminUser extends User{
public $password;
public function _ _construct($name, $birthday)
{
parent::_ _construct($name, $birthday);
$db = dba_popen("/data/etc/auth.pw", "r", "ndbm");
$this->password = dba_fetch($db, $name);
dba_close($db);
}
public function authenticate($suppliedPassword) 
{
if($this->password === $suppliedPassword) {
return true;
}
else {
return false;
}
}
}

Although it is quite short, AdminUser automatically inherits all the methods from User, so you can call hello(), goodbye(), and age(). Notice that you must manually call the constructor of the parent class as parent::_ _constructor(); PHP5 does not automatically call parent constructors. parent is as keyword that resolves to a class's parent class.

Encapsulation

Users coming from a procedural language or PHP4 might wonder what all the public stuff floating around is. Version 5 of PHP provides data-hiding capabilities with public, protected, and private data attributes and methods. These are commonly referred to as PPP (for public, protected, private) and carry the standard semantics:

  • PublicA public variable or method can be accessed directly by any user of the class.

  • ProtectedA protected variable or method cannot be accessed by users of the class but can be accessed inside a subclass that inherits from the class.

  • PrivateA private variable or method can only be accessed internally from the class in which it is defined. This means that a private variable or method cannot be called from a child that extends the class.

Encapsulation allows you to define a public interface that regulates the ways in which users can interact with a class. You can refactor, or alter, methods that aren't public, without worrying about breaking code that depends on the class. You can refactor private methods with impunity. The refactoring of protected methods requires more care, to avoid breaking the classes' subclasses.

Encapsulation is not necessary in PHP (if it is omitted, methods and properties are assumed to be public), but it should be used when possible. Even in a single-programmer environment, and especially in team environments, the temptation to avoid the public interface of an object and take a shortcut by using supposedly internal methods is very high. This quickly leads to unmaintainable code, though, because instead of a simple public interface having to be consistent, all the methods in a class are unable to be refactored for fear of causing a bug in a class that uses that method. Using PPP binds you to this agreement and ensures that only public methods are used by external code, regardless of the temptation to shortcut.



 
 
>>> More PHP Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: