Developers/Object oriented programming in PHP/Properties

How to initialize properties
There are three possibilities:


 * assignment:  allows you to assign a value to the class property directly, while   can be used everywhere inside the class.
 * constructor: Defining a method named __construct overrides the standard procedure of the new operator and enables to set certain values for certain properties automatically when a new object of the class is created.
 * setter methods: Setter and getter methods are mostly used by items in Arcavias which are objects storing data temporarily for passing it around inside the code.

Note: Please mind the Coding Standard: private declared properties (as well as private / protected methods) are always prefixed with an underscore.

For an item like MShop_Product_Item_Default this looks like this (there are corresponding setter / getter for each other value of course):

Notes about performance:
 * Don't assign a default value to a class property directly, if it's immediately overwritten by the constructor
 * In items use only one associative array of key/value pairs as it's returned by the database methods
 * Postpone retrieving and transforming the data until it's really needed, e.g. like getLabel does

See also:


 * Assignment: http://php.net/manual/en/language.operators.assignment.php
 * Constructors / Destructors: http://php.net/manual/en/language.oop5.decon.php

Visibility of properties

 * public : It's dangerous to declare a property as public, because everybody can change the value anytime and after initializing the property all control over the value gets lost. So don't use the public keyword for properties!
 * protected : Properties declared as protected</tt> can be accessed only in the definition of the actual class or of its subclasses. As a consequence not only every method of the class can operate on a protected</tt> property, but also every method of the subclasses. And this can lead to incompatibility of code, when subclasses override the protected</tt> properties of their superclasses. Use this with extreme care
 * private : All properties should be declared as private</tt> to ensure they can be only manipulated by methods of their own class in the class definition and to maintain full control of their values. private</tt> declared properties can be accessed only in the class definition of their own class, are excluded from inheritance to subclasses and in this way can't be overridden. This improves both consistency and security of the code.

Usage in Arcavias:

All properties of classes are declared as private</tt>. Class MShop_Product_Item_Default</tt> for instance defines one private property _values</tt> storing all data of the item in an array:

Please mind the Coding Standard: private</tt> declared properties (as well as private</tt> / protected</tt> methods) are always prefixed with an underscore.

See also:


 * http://php.net/manual/en/language.oop5.properties.php
 * http://php.net/manual/en/language.oop5.visibility.php

Static properties
A normal, non-static property is bound to a concrete object and each object has it's own "property instance" with a value that is not identical with the value of the same property of other objects. Static declared properties are initialized once at run-time, there can be only one "instance" of a static property with one value assigned to it.

The static</tt> keyword is not used very often in Arcavias for properties. In MShop</tt> it's used for include paths and it occurs in classes for unit tests, see class TestHelper</tt> in lib/mshoplib/tests/</tt> for example. The purpose of static properties is most often to cache objects that are shared by all instances of the class, e.g. in the TestHelper</tt> class:

Both properties are accessed in this class via the <tt>self::</tt> keyword:

See also:


 * http://php.net/manual/en/language.oop5.static.php

<< Previous: Classes | Next: Methods >>