Eclipse Templates
here are my favourite Eclipse templates. They make objective design faster and handle manu reoccuring patterns. One of the criticisms many people make of OOP is that it generates "too much code"; while that is kind of a delusion -- the size of basic accessors is irreveant if they are simply wrappers for single value access and modification -- it does get tedious to retype the same basic pattterns and these templates can really boost you away from that sort of RSI inducing work.
Note-- there's no reason to use my names; you can call these templates whatever you want.
Property (Name: property)
This basic get/set handler, with comment boundry.
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ${1} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
private $$_${1} = NULL;
/**
* @return scalar
*/
public function get_${1}(){ return $$this->_${1}; }
public function set_${1}($$value){ $$this->_${1} = $$value; }
Class Property (Name: clop)
Includes type hinting; if used with care you can "tab in" to the hint and select a class from the project.
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ${1} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
private $$_${1} = NULL;
/**
* @return ${class}
*/
public function get_${1}(){ return $$this->_${1}; }
public function set_${1}(${class} $$value){ $$this->_${1} = $$value; }
Method (Name: method)
An object encased function. The difference between properties and methods is functional, not structural. Methods in general are based on computations derived from a combination of variables (but not simply one single variable), where properties are glorified variables, which are at best filtered or constrained to a set of values or a datatype.
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ${variable} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
public function ${variable}()
{
return $$out;
}"If Is Null" property (Name: iin)
Handles the (quite common case) of a property that you "lazy load" once when it is first called, and whose value does not change once computed.
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ${1} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
private $$_${1} = NULL;
function get_${1}()
{
if (is_null($$this->_${1})):
// process
$$this->_${1} = $$value;
endif;
return $$this->_${1};
}Array Prop (Name: aprop)
A cluster of accessors for an array based property. Note that in many contexts only a few of these methods are necessary. It depends on whether you want to treat the array as a "stack" of values whose keys are irrelevant, or a hash or indexed list whose keys have meaning.
/* @@@@@@@@@@@@@@@@ ${1} @@@@@@@@@@@@@@@@@ */
private $$_${1} = array();
public function set_${1}s($$pValues, $$pAppend = FALSE)
{
if ($$pAppend):
foreach($$pValues as $$k => $$value):
$$this->_${1}[$$k] = $$value;
endforeach;
else:
$$this->_${1} = $$pValues;
endif;
}
/* ---------------- get_${1} s----------------------------- */
// get the entire array -- that is, a copy of the array that can be altered without affecting the original
public function get_${1}s(){ return $$this->_${1}; }
/* ----------------- cell set/get for ${1} --------------------- */
// retrieve or set array by key
public function set_${1}($$pKey, $$pValue){ $$this->_${1}[$$pKey] = $$pValue; }
public function get_${1}($$pKey, $$pDefault = NULL){
if (array_key_exists($$pKey, $$this->_${1})):
return $$this->_${1}[$$pKey];
else:
return $$pDefault;
endif;
}
/* ----------------- add_${1} ----------------------------- */
// add an item to ${1} as "stack". NOTE: you will NOT know its index!
public function add_${1}($$item) { $$this->_${1}[] = $$item; }
/* *
* These more obscure methods can be erased or left cloaked
* if they are not needed.
public function unset_${1}($$pKey){
if (array_key_exists($$pKey, $$this->_${1})):
unset($$this->_${1}[$$pKey]);
endif;
}
public function has_${1}($$pKey){ return array_key_exists($$pKey, $$this->_${1}); }
*/
Writing your own templates
These examples should give you a good idea of how to write tempates; but to emphasize the basic rules
- Dollar signs must be doubled ($$ = $) to display normally. Important as PHP has a lot of uses of the dollar sign.
- Placeholders are indicated by ${1} (and I suppose, ${2..N} though I've never gone past ${1}). If a user replace one instance of a variable, the template clones the type-in over each instance of the same variable.
- ${class} is a special placeholder that will load a dropdown of all classes.

Post new comment