Developers/Object oriented programming in PHP/Classes/en

Why using classes instead of functions?
A big advantage of classes is, that there's no need to deliver data as parameters to a method of a class. The method itself "knows" which data, i.e. objects, belongs to it. Whereas for a function all data it's supposed to process must be delivered as parameters. Nevertheless, methods can be defined to accept parameters in order to configure the processing of the object properties or to do something else.

Another disadvantage of functions is, that they have only one return value. They normally can't change other data external to the function content. But methods can operate on the data of their objects. Classes have properties the methods are working with, so calling a method for an object is enough, to tell the method which data should be processed.

How to extend classes
In general extensions of classes are used to add more functionality to the inheriting class or to change existing functionality of the superclasses implementing a new class derived from the parent class.

One class can extend only one parent class. It is not possible for a class to inherit from multiple parent classes.

Usage in Arcavias:

Generic functionality common to many different classes is provided by superclasses. This way specialised subclasses need not to reimplement already existing methods, they just define the new and special methods and inherite all common functionality by extending the proper superclass. Another advantage is, that the content of common methods can be easily changed by editing the superclasses, all subclasses can be left as they are.

Example:

Look at the header of the following classes, they all build a "chain of inheritance", from superclass to subclass.

MShop_Product_Item_Default <- MShop_Common_Item_ListRef_Abstract <- MShop_Common_Item_Abstract <- MW_Common_Item_Abstract

See also:


 * http://php.net/manual/en/language.oop5.basic.php -> extends

What general rules apply to subclasses?
For extension of classes following rules apply:


 * One class can extend only one parent class. It is not possible for a class to inherit from multiple parent classes.
 * Methods passed on to subclasses and overridden by them must have the same visibility scope (public or protected) like in parent classes.
 * In a class all public / protected properties / methods of the superclasses are visible, i.e. can be used in the subclass without having to define them again. Also all own properties / methods including the ones declared as private are visible, but of course must be defined first.
 * With the :: operator (scope resolution operator or double colon) it's possible to access overridden methods of non-direct superclasses in class definitions:.
 * parent:: refers to methods of the direct parent class:.
 * Constants and static methods / properties can be called also using the :: operator:  or  . The keywords self::</tt> and parent::</tt> are available, too. But parent::</tt> should be avoided in this context.

See also:


 * http://php.net/manual/en/language.oop5.basic.php -> extends
 * http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

How to design "good" classes and establish a well structured code
General rules:


 * Structure code into sense making parts of functionality
 * Create classes for reusable code

One class should handle one task only

 * One resource
 * Multiple methods managing that resource
 * No too extensive classes

Example in Arcavias:

MW_Logger_Zend</tt> is a good example for a class handling one resource. MW_Logger_Zend</tt> is responsible to write log entries and is the only class managing the Zend_Log</tt> resource:

Composition is better than extension

 * Don't create one "root class" and multiple subclasses extending each other for all purposes
 * Instead write multiple classes parallel to each other on the root level or the same hierarchical level
 * Use classes in other classes
 * Create classes managing objects for other classes
 * Consider using design patterns

Example compositon vs. inheritance:
 * Class A has m</tt> possibilities doing something.
 * Class B has n</tt> possibilities doing something.
 * Maximum number of possibilities using one parent class A and a direct subclass B is m + n</tt>.
 * Maximum number of possibilities using two parallel classes A and B on the same hierarchical level is m x n</tt>.

So again in other words: It's better to integrate and use objects in other classes instead of extending classes for all different combinations.

Example in Arcavias:

MShop_Catalog_Item_Default</tt> and MShop_Catalog_Manager_Default</tt> demonstrate how composition works:

The catalog item object holds a catalog node and it's children via the properties _node</tt> and _children</tt>. The item has only methods concerning it's own properties (mostly setter and getter methods). But the catalog manager takes care of the things that should be done with it defining all necessary methods, which are the standard methods for a manager to create, save, search, delete an item and the domain specific ones like to move or insert a catalog tree node or getting all catalog nodes lying in the path to a given catalog node. Item and manager are establishing the DAO / DTO design pattern.

Next: Properties >>