tags:

views:

128

answers:

4

Hello; I've got an Object Oriented library I wanted to add a method to, and while I'm fairly certain I could just go into the source of that library and add it, I imagine this is what's generally known as A Bad Idea.

How would I go about adding my own method to a PHP object correctly?

UPDATE ** editing **

The library I'm trying to add a method to is simpleHTML, nothing fancy, just a method to improve readability. So I tried adding to my code:

class simpleHTMLDOM extends simple_html_dom {
  public function remove_node() {
    $this->outertext = "";
  }
}

which got me: Fatal error: Call to undefined method simple_html_dom_node::remove_node(). So obviously, when you grab an element in simpleHTML it returns an object of type simple_html_dom_node.

If I add the method to simple_html_dom_node my subclass isn't what will be created by simplHTML ... so stuck as to where to go next.

+6  A: 

A solution would be to create a new class, that extends the one from your library -- and, then, use your class, which have have all methods of the original one, plus yours.

Here's a (very quick and simple) example :

class YourClass extends TheLibraryClass {
  public function yourNewMethod() {
    // do what you want here
  }
}

And, then, you use your class :

$obj = new YourClass();
$obj->yourNewMethod();

And you can call the methods of the TheLibraryClass class, as yours inherits the properties and methods of that one :

$obj->aMethodFromTheLibrary();


About that, you can take a look at the Object Inheritance section of the manual.


And, as you guessed, modifying a library is definitly a bad idea : you'll have to re-do that modification each time you update the library !

(One day or another, you'll forget -- or one of your colleagues will forget ^^ )

Pascal MARTIN
Thanks, still having issues, made an edit above. Honestly just dropping my method into the source works but I'd like to do it "right" if I can.
Stomped
A: 

I don't why adding the method would be bad, however if you want to so without editing the library, your best bet would be to extend the class like so:

class NewClass extends OldClass {

  function newMethod() {
    //do stuff
  }

}
GSto
Adding the method is bad because if you update the library, or the library changes its internal implementation, you are very apt to have compatibility issues.
Chacha102
Yep, but I can't get this to work by extending the class, so far. Hopefully someone can help based on my edits.
Stomped
+1  A: 
class myExtenstionClass extends SomeClassInLibrary
{

  public function myMethod()
  {
     // your function definition
  }
}

As Pascal suggests... read the manual :-)

prodigitalson
+2  A: 

You could do it with inheritance, but you could also use a decorator pattern if you do not need access to any protected members from SimpleHtml. This is a somewhat more flexible approach. See the linked page for details.

class MySimpleHtmlExtension
{
     protected $_dom;

     public function __construct(simple_html_dom $simpleHtml)
     {
         $this->_dom = $simpleHtml;
     }

     public function removeNode(simple_html_dom_node $node)
     {
         $node->outertext = '';
         return $this;
     }

     public function __call($method, $args)
     {
         if(method_exists($this->_dom, $method)) {
             return call_user_func_array(array($this->_dom , $method), $args));
         }
         throw new BadMethodCallException("$method does not exist");
     }

}

You'd use the above like this

$ext = new MySimpleHtmlExtension( new simple_html_dom );
$ext->load('<html><body>Hello <span>World</span>!</body></html>');
$ext->removeNode( $ext->find('span', 0) );
Gordon
Here's the problem - the method actually gets called on a `simple_dom_html_node` object - which is created by the `simple_dom_html` object. I don't think there's a solution.
Stomped