Searching...
Wednesday 24 July 2013

Defining Class Properties in PHP


Properties are attributes that are intended to describe some aspect of a class. They are quite similar to standard PHP variables (), except for a few minor differences, which you’ll learn about in this article. You’ll also learn and .

Declaring Properties

The rules regarding property declaration are quite similar to those in place for variable declaration (); essentially, there are none. Because PHP is a loosely typed language, properties don’t even necessarily need to be declared; they can simply be created and assigned simultaneously by a class object, although you’ll rarely want to do that.

For introduction to classes and objects in PHP refer to my previous post Key concepts of OOP in PHP

Instead, common practice is to declare properties at the beginning of the class. Optionally, you can assign them initial values at this time. An example follows:

class Employee
{
public $name = "John";
private $wage;
}

In this example, the two properties, name and wage, are prefaced with a (public or private), a common practice when declaring properties. Once declared, each property can be used under the terms accorded to it by the scope descriptor. If you don’t know what role scope plays in class properties, don’t worry, this topic is covered later in this article.

Invoking Properties

Properties are referred to using the -> operator and, unlike variables, are not prefaced with a dollar sign. Furthermore, because a property’s value typically is specific to a given object, it is correlated to that object like this:

$object->property

For example, the Employee class includes the properties name, title, and wage. If you create an object of type Employee, you would refer to its properties like this:

$employee->name
$employee->title
$employee->wage

When you refer to a property 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 property residing in the same class in which the property is being accessed or manipulated. Therefore, if you were to create a method for setting the name property in the Employee class, it might look like this:

function setName($name)
{
$this->name = $name;
}

Property Scopes

PHP supports five class property scopes: public, private, protected, final, and static. The first four are introduced in this article, and the static scope is introduced in the article “More about Object Oriented PHP”.

Public

You can declare properties in the public scope by prefacing the property with the keyword public. An example follows:

class Employee
{
public $name;
// Other property and method declarations follow...
}

Public properties can then be accessed and manipulated directly via the corresponding object, like so:

$employee = new Employee();
$employee->name = "Mary Swanson";
$name = $employee->name;
echo "New employee: $name";

Executing this code produces the following:

New employee: Mary Swanson

Although this might seem like a logical means for maintaining class properties, public properties are actually generally considered taboo to OOP, and for good reason. The reason for shunning such an implementation is that such direct access robs the class of a convenient means for enforcing any sort of data validation. For example, nothing would prevent the user from assigning name like so:

$employee->name = "12345";

This is certainly not the kind of input you are expecting. To prevent such occurrences, two solutions are available. One solution involves encapsulating the data within the object, making it available only via a series of interfaces, known as public methods. Data encapsulated in this way is said to be private in scope. The second recommended solution involves the use of properties and is actually quite similar to the first solution, although it is a tad more convenient in most cases.

Private

Private properties are only accessible from within the class in which they are defined. An example follows:

class Employee
{
private $name;
private $telephone;
}

Properties designated as private are not directly accessible by an instantiated object, nor are they available to child classes (more about child classes in OOP model of PHP). If you want to make these properties available to child classes, consider using the protected scope instead, introduced next. Note that private properties must be accessed via publicly exposed interfaces, which satisfies one of OOP’s main tenets: encapsulation. Consider the following example, in which a private property is manipulated by a public method:

class Employee
{
private $name;
public function setName($name) {
$this->name = $name;
}
}
$employee = new Employee;
$employee->setName("Mary");

Encapsulating the management of such properties within a method enables the developer to maintain tight control over how that property is set. For example, you could add to the setName() method’s capabilities to validate that the name is set to solely alphabetical characters and to ensure that it isn’t blank. This strategy is much more reliable than leaving it to the end user to provide valid information.

Protected

Just like functions often require variables intended for use only within the function, classes can include properties used for solely internal purposes. Such properties are deemed protected and are prefaced accordingly. An example follows:

class Employee
{
protected $wage;
}

Protected properties are also made available to inherited classes for access and manipulation, a trait not shared by private properties. Any attempt by an object to access a protected property will result in a fatal error. Therefore, if you plan on extending the class, you should use protected properties in lieu of private properties.

Final

Marking a property as final prevents it from being overridden by a subclass. A finalized property is declared like so:

class Employee
{
final $ssn;
}


0 comments:

Post a Comment

 
Back to top!