4

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.

2
  • 2
    Not sure why this got downvoted, so here's +1 Commented Jan 27, 2010 at 17:33
  • Could you add the code that is instantiating the object and calling the remove_node() method? Commented Jan 27, 2010 at 21:59

4 Answers 4

6

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 ^^ )

Sign up to request clarification or add additional context in comments.

1 Comment

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

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) ); 

1 Comment

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

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 } } 

2 Comments

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.
Yep, but I can't get this to work by extending the class, so far. Hopefully someone can help based on my edits.
0
class myExtenstionClass extends SomeClassInLibrary { public function myMethod() { // your function definition } } 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.