Oops, I left this in my Drafts folder, sorry about that.

On Wed, 2004-02-04 at 20:28, Vivian Steller wrote:
> ok my problem in a nutshell: i need multiple inheritance! is there a
> solution to fake this functionallity in php5?

It sounds like you would want to check out association and
aggregation[1].  Though aggregation looks mighty attractive, I doubt it
will work in your situation since you do not get the constructor, and
therefore the underlying reference to the DOM library is missing.  See
below for a possible solution.

> i have the following problem:
> the goal is to implement my own DomXML implementation based on the existing
> DomNode, DomDocument, Dom... Classes.
> 
> thus, i implement my classes as follows:
> 
> class MyNode extends DomNode {}                 // that isn't the problem
> class MyDocument extends DomDocument {}         // would be fine, but...
> 
> this would be an easy solution,  but there is a logical problem:
> if i extend the DomNode (with the methods in MyNode) then -i think-
> MyDocument should have those extended methods, too.
> so i could write:
> 
> class MyDocument extends MyNode {}              // problem
> 
> but now i'm missing all the nice methods of DomDocument!!
> multiple inheritance would solve this problem:
> 
> class MyDocument extends MyNode,DomDocument {}  // not possible

Neither prototype nor multiple inheritance is directly supported in
php.  The best option to achieve what you want would be to define your
new class layer (MyNode, MyDocument, etc.) as inheriting from their
respective classes (ex. MyDocument extends MyNode).  Then within each
class definition hold the respective DOM class in a property:
class MyNode {
  var $dom_node;
  function MyNode($param) {
    $this->dom_node = new DomNode($param);
  }
}

You then will need to write stub methods to implement each method in the
DOM class for your class.  You can then manually override any DOM method
as well as extend the class in your own manner.  An important note: You
*never* want to access the property for the DOM instance directly, if
you are using PHP 5 you *must* make this property private.  This is a
fair amount of code bloat but afaik the only way you can conform php4 to
your design.  See below for more solutions relating to PHP 5.

> I'm thinking about the new __call() method implemented as follows:
> 
> class MyDocument extends MyNode {
>         function __call($method, $params) {     // fake solution
>                 ...
>                 eval("DomDocument::$method($paramsString);");
>         }
> }
> 
> but with a fake solution like this i wouldn't get all methods with
> get_class_methods() and that wouldn't be that nice:)

The Reflection API[2] in PHP 5 can help you here.  I'm hoping to play
with it sometime and see if it can be used to fake prototypes by
allowing modification of the method and property spaces.  That *would*
be nice. :)  Even if it does not, you can still use it to look up and
call methods from other classes.

> Can Interfaces help me?

They may, but interfaces will not implement the code themselves. 
Without prototypes you will need to either write accessor code to the
DOM methods in a function outside your classes and a wrapper for each
object, or copy and paste the code manually to implement the additional
Node sub-class functions across all other sub-classes.  I would
recommend leaving interfaces out of this as they will provide
unnecessary structure and rigidity to your code.  Implement only the
constraints you absolutely need.

Hopefully this gives you something to think on.  As I am still working
out the possibilities in php myself I would appreciate any findings you
have back here.

Hope this helps,
Adam

[1] http://www.php.net/ref.objaggregation
[2]
http://sitten-polizei.de/php/reflection_api/docs/language.reflection.html

-- 
Adam Bregenzer
[EMAIL PROTECTED]
http://adam.bregenzer.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to