compared with
Current by Walter Tamboer
on May 17, 2012 08:45.

(show comment)
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (24)

View Page History
h3. Introduction
<h3>Introduction</h3>

<p>There has been quite some discussion about the naming convention of class methods. This discussion is mainly focused on whether or not method names should be prefixed with a "get" &quot;get&quot; and "set" prefix. &quot;set&quot; prefix.</p>

<p>As seen in the example below there is a mixture of using getters methods with the get prefix (getRequest, getResponse) and without (events). There is no right and wrong here, it's just a matter of preference. Since Zend Framework is a library and is used by many people, we should be consistent with the naming convention and that is the reason why this RFC is here. Let's take a look at the example and than create a list with pros and cons of both naming conventions.</p>

h3. Example
<h3>Example</h3>

{code:php|title=https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Controller/ActionController.php#L135}
<ac:macro ac:name="code"><ac:parameter ac:name="title">https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Controller/ActionController.php#L135</ac:parameter><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
/**
* Get the request object
// ...
}
{code}
]]></ac:plain-text-body></ac:macro>

h3. Advantages for using the "get" prefix for method names
<h3>Advantages for using the &quot;get&quot; prefix for method names</h3>

<ol>
# The <li>The function name is consistent with the set equivalent (getRequest <-> &lt;-&gt; setRequest, is one of the two missing than we immediately know that it's not possible).</li>
# Functions <li>Functions are actions (something is done) and should therefore be named with a verb. Taking that into account makes "Person::getName()" &quot;Person::getName()&quot; valid while "Person::name()" &quot;Person::name()&quot; is not. (ok name can also be used as a verb but not in this context). PHP does not support properties and leaving the get prefix out actually means that we are trying to simulate them. In the example below (request vs getRequest) you can see how confusing that can be.</li>
# It's <li>It's easier to use within an IDE. Once you type "$instance->get", &quot;$instance-&gt;get&quot;, your IDE will give a list with all possible getters. In my opinion a VERY important point since ZF is a public library.</li>
# The <li>The function implies what it does. Take the following example:
{code:php}
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
/**
* Get the request object
// It's clear that a request is returned.
}
]]></ac:plain-text-body></ac:macro></li>
{code} </ol>

h3. Disadvantages for using the "get" prefix for method names

# It's longer to type. An approach like $company->employees()->fire(); might feel more fluent.
<h3>Disadvantages for using the &quot;get&quot; prefix for method names</h3>

<ol>
<li>It's longer to type. An approach like $company-&gt;employees()-&gt;fire(); might feel more fluent.</li>
</ol>