Jump to content


Photo

OOP issue: How to solve this


  • Please log in to reply
28 replies to this topic

#21 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 August 2006 - 06:48 AM

I used to use perl for this kind of programming so am really happy I stumbled across PHP. However, they lost a few points in my book for this.


It's not PHP who designed the structure, but W3C. These classes follow the DOM level 2 recommendation as closely as possible.

Thanks for all the help, later.  :)

#22 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 August 2006 - 06:58 AM

In all honesty, if its OOP your fond of, php is only just comming to grips with it. I haven't really followed your thread but maybe a more OO freindly language like Python or Ruby is what your craving.

#23 janxyzphp

janxyzphp
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 September 2006 - 09:29 AM

Also want to extend the DomDocument.... Could a Decorator class help here? Anyone tried this?
J

#24 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 10:21 AM

Also want to extend the DomDocument.... Could a Decorator class help here? Anyone tried this?
J


You can extend DOMDocument, no props. The problem arises when you want  to create a document from scratch with a doctype declarations , you need DOMImplementation. Some of the properties defining a document are read-only , and thus can't be overwritten.

Even stranger, the're appearantly not just 'final', but something else is going on, Reflection doesn't return ANY properties...

You can extend DOMDocument fine, and load doctype declarations with loadXML().

Or just use propagation, like I eventually was forced to do.

#25 janxyzphp

janxyzphp
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 September 2006 - 06:52 PM

Yep, that did not work for me either.

So i declared a new class XxDomDocument and encapsulated the DomImplementation and DomDocument in the constructor. (U can add params to the constructor if u want and pass to the DomImplementation u are creating).
The resulting object, this->doc, i then 'decorate' with the php5 magic functions _set, _get and _call.
The resulting XxDomDocument for all intends and purposes is the same as a DomDucement created by the DomImplementation. Now u can add in or extend the XxDomDocument class with whatever methods u desire.

is looks a bit like so:
class XxDomDocument {
protected $doc;

public function __construct () {
$DOM = new DOMImplementation();
$dtd = $DOM->createDocumentType ("html" , "-//W3C//DTD XHTML 1.0 Strict//EN" , "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
$this->doc = $DOM->createDocument ( null, null, $dtd);
$this->doc->formatOutput = true;
$this->doc->preserveWhiteSpace = false;
}

public function __set( $name, $value )
{
if( property_exists( $this->doc, $name ) )
$this->doc->$name = $value;
}

public function __get( $name )
{
if( property_exists( $this->doc, $name ) )
return $this->doc->$name;
}

public function __call( $name, $params )
{
if( method_exists( $this->doc, $name ) )
return call_user_func_array( array( $this->doc, $name ), $params );
}
// end 

}


I can validate and use getElementById.
I can add for example a method to print a page without the xml declaration which would throw WINIE6 into quirksmode...

J

#26 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 15 September 2006 - 07:46 PM

Was this not the conclusion we came to earlier;

We can call the methods, but don't have access to the properties, and in order to simulate it, you will need to declare an instance in your custom object and wrap the method calls to pull from the instantiated object?

I would like to know if this is no longer the case, because I can use the method in my current project. I have a bunch of objects that have to pull data from an instantiated object.

regards,
...drkstr

**edit**
Although I see your point, it is a pretty handy way to copy all the methods to your own class. I'm not sure what the original posters intentions were, but from my understanding, I think the problem was being able to extend the object, not use it in another object. I am interested to see if there is a way to modify the data, or if you are limitted to modyfying it with the provided methods.

#27 janxyzphp

janxyzphp
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 September 2006 - 08:38 PM

eh, not sure, i thought it was more about trying to import and/or overwrite the properties of the instance.

with the Magic _get, _set and _call XxDomDocument allows me transparent access to all properties and methods of the encapsulated DomDocument instance.

usage like so:
$doc = new XxDomDocument();
$doc->validatedOnParse = true;
$doc-Load('page.html');
$node = $doc->getElementById('myid');
$docElement = $doc->documentElement;
(of course u'd have to modify the doctype settings in the previous code snippet to ur liking)

But perhaps i misread the thread and my suggestion is redundant.
Jan

#28 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 08:41 PM

Yep, that did not work for me either.

So i declared a new class XxDomDocument and encapsulated the DomImplementation and DomDocument in the constructor. (U can add params to the constructor if u want and pass to the DomImplementation u are creating).
The resulting object, this->doc, i then 'decorate' with the php5 magic functions _set, _get and _call.
The resulting XxDomDocument for all intends and purposes is the same as a DomDucement created by the DomImplementation. Now u can add in or extend the XxDomDocument class with whatever methods u desire.

is looks a bit like so:

class XxDomDocument {
protected $doc;

public function __construct () {
$DOM = new DOMImplementation();
$dtd = $DOM->createDocumentType ("html" , "-//W3C//DTD XHTML 1.0 Strict//EN" , "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
$this->doc = $DOM->createDocument ( null, null, $dtd);
$this->doc->formatOutput = true;
$this->doc->preserveWhiteSpace = false;
}

public function __set( $name, $value )
{
if( property_exists( $this->doc, $name ) )
$this->doc->$name = $value;
}

public function __get( $name )
{
if( property_exists( $this->doc, $name ) )
return $this->doc->$name;
}

public function __call( $name, $params )
{
if( method_exists( $this->doc, $name ) )
return call_user_func_array( array( $this->doc, $name ), $params );
}
// end 

}


I can validate and use getElementById.
I can add for example a method to print a page without the xml declaration which would throw WINIE6 into quirksmode...

J


The magic mehods. Why I didn't put two and two together... I guess drstr and I were to focussed on the inheritance issues to see this overriding oppertunity.

Thanks for putting the light switch on.  :D

We can call the methods, but don't have access to the properties, and in order to simulate it, you will need to declare an instance in your custom object and wrap the method calls to pull from the instantiated object?


That is exactly what he is doing with the magic mehods. Except would you before be required to implicitly simulate everything, with the magic methods wou can suffice with only three methods to simulate getting/ setting all properties and executing all methods.

We (or I) tried to extend DomDocument, and import properties from a DOMDocument instance created by DOMIplementation. The importing being the weak link.

I would still prefer inheritance, but this is at least a more convenient solution than redefining all methods and properties one wants to 'relay'.

#29 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 16 September 2006 - 12:19 AM

heh, yeah, that would be quite a chore. And it is a lot better then my "array of function references" idea too. I'll have to remember these, could come in handy some day.

...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users