Overload Classes: Difference between revisions
New page: {{Howto|Intermediate|Thomas Holz <vhskids@gmx.de>}} == Intro == An overloader is a way of replacing one of the classes your app normally uses, with one of your own. A plain text examp... |
|||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 5: | Line 5: | ||
A plain text example: "Use mymodule.mycustomprojectnode whereever the app normally uses the projects.project node". | A plain text example: "Use mymodule.mycustomprojectnode whereever the app normally uses the projects.project node". | ||
In other words, it is a way of modifying the app, without touching its code. | In other words, it is a way of modifying the app, without touching its code. | ||
'''Note:''' This method is for overloading nodes only. for overloading base classes try atkClassLoader(). | |||
== Example == | == Example == | ||
To create an overloader, put this in your module.inc, near the top of the file (before the module definition): | To create an overloader, put this in your module.inc, near the top of the file (before the module definition): | ||
| Line 10: | Line 13: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
In your mymodule module, create the node mynode, like this: | In your mymodule module, create the node mynode, like this: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
atkimport("module.project.project"); | |||
class mynode extends project | class mynode extends project | ||
{ | { | ||
function action_admin() | public function action_admin() | ||
{ | { | ||
echo "Hello World"; | echo "Hello World"; | ||
} | } | ||
} </syntaxhighlight> | } | ||
</syntaxhighlight> | |||
In this example, the project node is replaced by a mynode node, which derives from project, but overrides its action_admin behaviour. | In this example, the project node is replaced by a mynode node, which derives from project, but overrides its action_admin behaviour. | ||
== Criticism == | |||
The disadvantage of node overloaders is that due to the restrictions of single inheritance, it's not possible to overload a node twice. If a node is overloaded twice, the first overloader will be ignored. This makes the feature less useful for [[Creating add-on modules|Add-on Modules]]. It is therefore recommended to use overloaders sparingly, preferably only to create project-specific customisation modules. In almost all other cases, using [[Modifiers]] is a better, more flexible, solution. | |||
== Conclusion == | == Conclusion == | ||
It's not strictly necessary to derive from the original class, but this ensures that the overloader is compatible with the original. (The code in the application relies on the project node having certain attributes and behaviour, and by deriving from that class, we preserve this as much as possible.) | It's not strictly necessary to derive from the original class, but this ensures that the overloader is compatible with the original. (The code in the application relies on the project node having certain attributes and behaviour, and by deriving from that class, we preserve this as much as possible.) | ||
Latest revision as of 07:42, 10 February 2010
|
ATK Howto: Overload Classes
|
Intro
An overloader is a way of replacing one of the classes your app normally uses, with one of your own. A plain text example: "Use mymodule.mycustomprojectnode whereever the app normally uses the projects.project node". In other words, it is a way of modifying the app, without touching its code.
Note: This method is for overloading nodes only. for overloading base classes try atkClassLoader().
Example
To create an overloader, put this in your module.inc, near the top of the file (before the module definition):
$overloaders["project.project"] = "mymodule.mynode";
In your mymodule module, create the node mynode, like this:
atkimport("module.project.project");
class mynode extends project
{
public function action_admin()
{
echo "Hello World";
}
}
In this example, the project node is replaced by a mynode node, which derives from project, but overrides its action_admin behaviour.
Criticism
The disadvantage of node overloaders is that due to the restrictions of single inheritance, it's not possible to overload a node twice. If a node is overloaded twice, the first overloader will be ignored. This makes the feature less useful for Add-on Modules. It is therefore recommended to use overloaders sparingly, preferably only to create project-specific customisation modules. In almost all other cases, using Modifiers is a better, more flexible, solution.
Conclusion
It's not strictly necessary to derive from the original class, but this ensures that the overloader is compatible with the original. (The code in the application relies on the project node having certain attributes and behaviour, and by deriving from that class, we preserve this as much as possible.)
original from: http://forum.achievo.org/forum/viewtopic.php?t=1764