Jump to content

Archived

This topic is now archived and is closed to further replies.

448191

OOP issue: How to solve this

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote author=janxyzphp link=topic=104013.msg435087#msg435087 date=1158346341]
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:
[code]
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

}

[/code]

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
[/quote]

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

[quote=drstr]
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?[/quote]

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'.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.