| 1 |
@sequence --- |
| 2 |
@title org.netfrag.yakka - ApiDoc available - powered by phpDocumentor |
| 3 |
@file C:\home\amo\develop\netfrag.org\joko\doc\twingled\SampleNewsPosts\Re - Namespaces in PHP.twingle |
| 4 |
@link http://grasshopper.netfrag.org/c/home/amo/develop/netfrag.org/joko/doc/twingled/SampleNewsPosts/Re - Namespaces in PHP.twingle |
| 5 |
@link http://www.netfrag.org/webnews/article.php?id=15&group=nfo.dev.php |
| 6 |
@link http://news.netfrag.org/nfo.dev.php/id/15/ |
| 7 |
|
| 8 |
Hey bareface, |
| 9 |
|
| 10 |
David Bareface wrote: |
| 11 |
> [...] |
| 12 |
> I checked out your approach for this issue and it's quite interesting. imho |
| 13 |
> this is a good way to avoid class redeclarations. |
| 14 |
> maybe syntaxing could be better. use a static class to get instances |
| 15 |
> maybe something like this: |
| 16 |
> |
| 17 |
> /* just pseudo-code */ |
| 18 |
> |
| 19 |
> class php { |
| 20 |
> function getInstance($class) { |
| 21 |
> global $uns; |
| 22 |
> $class = $uns."_".$class; |
| 23 |
> return new $class; |
| 24 |
> } |
| 25 |
> |
| 26 |
> function useNamespace($ns) { |
| 27 |
> global $uns; |
| 28 |
> $uns = $ns; |
| 29 |
> } |
| 30 |
> } |
| 31 |
> |
| 32 |
> php::useNamespace("Application"); |
| 33 |
> $config = php::getInstance("Config"); |
| 34 |
> |
| 35 |
> cu |
| 36 |
> bare |
| 37 |
|
| 38 |
Thanksalot for this! |
| 39 |
|
| 40 |
Yesterday I tried to mungle some of already |
| 41 |
existing code from org.netfrag.flib towards |
| 42 |
your proposal - what came out was: |
| 43 |
|
| 44 |
|
| 45 |
--- The php and the Exporter classes. |
| 46 |
|
| 47 |
syntaxing inspired by bareface: (e.g.) |
| 48 |
|
| 49 |
php::useNamespace("Data::Driver"); |
| 50 |
$config = php::getInstance("Proxy"); |
| 51 |
|
| 52 |
|
| 53 |
@sequence |
| 54 |
@title What did we do? |
| 55 |
@is-a RefactoringProcess |
| 56 |
|
| 57 |
What did we do? |
| 58 |
|
| 59 |
We tried to refactor some code to a newly |
| 60 |
introduced class named 'php': |
| 61 |
|
| 62 |
@subsequence |
| 63 |
@title What do we get? |
| 64 |
@is-a RefactoringTarget |
| 65 |
@class php |
| 66 |
|
| 67 |
|
| 68 |
@subsequence |
| 69 |
@title What did we have? |
| 70 |
@is-a RefactoringSource |
| 71 |
|
| 72 |
What did we have? |
| 73 |
|
| 74 |
Some code living at org.netfrag.flib: |
| 75 |
|
| 76 |
@package org.netfrag.flib |
| 77 |
@file flib/utils/helper.php |
| 78 |
@file flib/utils/shortcuts.php |
| 79 |
@function loadModule |
| 80 |
@function mkObject |
| 81 |
|
| 82 |
Some code living at org.netfrag.glib: |
| 83 |
|
| 84 |
@package org.netfrag.glib |
| 85 |
@subpackage DesignPattern |
| 86 |
@file ...rag.glib/DesignPattern/Bridge.php |
| 87 |
@component DesignPattern::Bridge |
| 88 |
@class DesignPattern_Bridge |
| 89 |
@methods '_mkInstance' and '_mkEmbeddedObjects' |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@subsequence |
| 94 |
@title What do we have *now*? |
| 95 |
@is-a RefactoringTarget |
| 96 |
@class php Exporter |
| 97 |
|
| 98 |
|
| 99 |
What do we have *now*? |
| 100 |
|
| 101 |
Some code living at org.netfrag.glib: |
| 102 |
|
| 103 |
@file ...rag.glib/php_extensions.php |
| 104 |
@component php_extensions |
| 105 |
@class php |
| 106 |
@methods loadModule, mkInstance, mkComponent |
| 107 |
|
| 108 |
@file ...rag.glib/Exporter.php |
| 109 |
@component Exporter |
| 110 |
@class Exporter |
| 111 |
|
| 112 |
The Exporter is used to alias php::mkComponent |
| 113 |
to the former global function mkObject. |
| 114 |
|
| 115 |
@subsequence |
| 116 |
@synopsis The classes can be used this way: |
| 117 |
|
| 118 |
<code> |
| 119 |
|
| 120 |
php::loadModule('my::Module'); |
| 121 |
|
| 122 |
$comp = php::mkComponent('my::Module'); |
| 123 |
|
| 124 |
$obj = php::mkInstance('my_Class'); |
| 125 |
|
| 126 |
</code> |
| 127 |
|
| 128 |
|
| 129 |
Of course, additional arguments can be fed |
| 130 |
to the constructors in case of object- |
| 131 |
instantiation (mkComponent and mkInstance). |
| 132 |
Just pass them as additional arguments... |
| 133 |
|
| 134 |
<code> |
| 135 |
|
| 136 |
$comp = php::mkComponent('my::Module', array( opt1 = >'hello' ), 'arg2' ); |
| 137 |
|
| 138 |
</code> |
| 139 |
|
| 140 |
greets, joko. |
| 141 |
|
| 142 |
|
| 143 |
p.s.: |
| 144 |
To actually implement your proposal one-by-one: |
| 145 |
|
| 146 |
Using the Exporter, it should be quite easy |
| 147 |
to alias php::mkComponent to php::getInstance. |
| 148 |
Adding your proposed php::useNamespace, the |
| 149 |
missing functionality (provided by php::getInstance) |
| 150 |
would be added as a fallback mechanism at |
| 151 |
the top of php::mkComponent: |
| 152 |
+ prefix modulename with namespace if desired (set by 'useNamespace') |
| 153 |
|
| 154 |
Here is the glitch: We actually have two *different* |
| 155 |
types of namespaces following this proposed component |
| 156 |
architecture: |
| 157 |
- ClassNamespace |
| 158 |
- ModuleNamespace |
| 159 |
|
| 160 |
|
| 161 |
[...] again - the post scriptum became way too long, |
| 162 |
so got refactored to a seperate document posted elsewhere - |
| 163 |
a reference will be added later.... |
| 164 |
The title will be: "DummyNamespaces in PHP - final proposal" [...] |
| 165 |
|
| 166 |
But of course i still forgot to add some links, here they are: |
| 167 |
@topic source(cvs) |
| 168 |
@link http://cvs.netfrag.org/nfo/php/libs/org.netfrag.glib/php_extensions.php |
| 169 |
@link http://cvs.netfrag.org/nfo/php/libs/org.netfrag.glib/Exporter.php |
| 170 |
|
| 171 |
@topic doc(api) |
| 172 |
@link http://api.netfrag.org/ |
| 173 |
@link http://netfrag.org/docs/code/api/org.netfrag/org.netfrag.glib/php.html |
| 174 |
@link http://netfrag.org/docs/code/api/org.netfrag/org.netfrag.glib/Exporter.html |