Developers/Object oriented programming in PHP/Methods

Visibility of methods

 * public
 * Methods declared public can be used everywhere, inside or outside of their or other classes and are inherited by subclasses. They belong to the external interface of a class and you can't change their signature without breaking compatibility to existing code, except adding more optional parameters. Public methods should normally be enforced by interfaces, so you should be sure that the methods won't change for a very long time. The amount of code in public methods should be small by sourcing out related code to protected methods to ease replacing the implemented functionality.


 * protected
 * protected methods can be accessed exclusively by methods of their own class or from subclasses. They belong to the internal interface of a class and changing their signature will also break compatibility (not if only optional parameters are added). Most of the methods used internally should be declared protected if you think their signature will be stable. They are a great way to split big methods into smaller pieces and enable subclasses to overwrite only that part of the functionality.


 * private
 * All private methods can be used only in their own class and they are not inherited by subclasses. You can change them whenever you like without affecting code outside the class. Use them if you are not sure that the method is not good/generic enough to be used by subclasses or if you think you will change that method in the future.

Notes: Please mind the Coding Standard for naming private and protected methods (with leading underscore).

See also:


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

Static methods
Methods declared as static</tt> are not bound to a concrete object of the class and can only be executed by using the scope resolution operator ::</tt>. self</tt> (self::myMethod</tt>), parent</tt> (parent::myMethod</tt>) or a name of an available class (MyClass::myMethod</tt>) are valid working with the operator. Static methods can not use $this</tt> in their definitions, because they are not invoked by an object, where $this</tt> would be the reference to. They can't operate on an object's normal properties, but they can (and normally do) operate on static properties.

static</tt> methods are mostly used by the factory design pattern. The class TestHelper</tt> in lib/mshoplib/tests/</tt> illustrates, how it works. The static method getContext</tt> returns the context object stored in a static property and the method also creates a new context object, if it exists not yet:

Method getContext</tt> is used in the unit tests to build a new context object for test managers. The method is invoked through the resolution scope operator ::</tt> with the classname as prefix:

See also:


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

Use type hinting for parameters
Type hinting means to declare the type of each parameter in the signature of the method. Then, a parameter passed to the called method is only accepted if it's type matches the given type in the signature of the method definition. This makes it possible to catch parameters errors early.

The following types can be specified for parameters: classes, interfaces, array</tt>. It's not possible to specify scalar types like <tt>int</tt> or <tt>string</tt> as parameter types.

All objects implementing the <tt>MShop_Context_Item_Interface</tt> are accepted by the constructor as parameter. <tt>MShop_Context_Item_Default</tt> is such an implementation, so all objects of this class are accepted.

This also applies to method <tt>saveItem</tt>, which demands an object of type <tt>MShop_Common_Item_Interface</tt>. So all implementations of this interface are accepted by the method. But this interface is implemented by all item classes of all domains and as a consequence every item will pass the type hinting test. The reason, that this general interface is chosen as type is, that methods common to all managers must have identical parameter signatures, because the same abstract class is extended and the same interfaces are implemented by all managers to gain control over each manager. To avoid that items of other domains are processed by the method a simple if clause checks, if the passed item is of the right type, <tt>MShop_Product_Item_Interface</tt> in this case.

See also:


 * http://php.net/manual/en/language.types.php
 * http://php.net/manual/en/language.oop5.typehinting.php

<< Previous: Properties | Next: Objects >>