How to add a method to an object in PHP?

I have an object-oriented library into which I wanted to add a method, and while I'm sure I can just go to the source of this library and add it, I think this is what is commonly known as the β€œBad” Idea.

How can I correctly add my own method to a PHP object?

UPDATE ** editing **

The library I'm trying to add a method to is simple HTML, nothing unusual, just a method to improve readability. So I tried adding in my code:

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

who 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 a method to simple_html_dom_node , my subclass will not be created using simpleHTML ... so stuck on where to go next.

+4
source share
4 answers

The solution is to create a new class that extends it from your library, and then use your class that has all the methods of the source, plus yours.

Here is an example (very quick and simple):

 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 methods of the TheLibraryClass class, since yours inherit the properties and methods of this:

 $obj->aMethodFromTheLibrary(); 


You can find this in the Object Inheritance section of the manual.


And, you guessed it, changing the library is definitely a bad idea: you will have to repeat this modification every time you update the library!

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

+6
source

You can do this with inheritance, but you can also use the decorator template if you don't need access to protected members from SimpleHtml. This is a slightly 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"); } } 

Would you use 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) ); 
+3
source

I don’t understand why adding a method would be bad, however, if you want it without editing the library, it would be best to extend the class as follows:

 class NewClass extends OldClass { function newMethod() { //do stuff } } 
0
source
 class myExtenstionClass extends SomeClassInLibrary { public function myMethod() { // your function definition } } 

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

0
source

Source: https://habr.com/ru/post/1299574/


All Articles