Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a method to an object in PHP?

Tags:

oop

php

class

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.

like image 745
Stomped Avatar asked Jan 23 '23 16:01

Stomped


2 Answers

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

like image 104
Pascal MARTIN Avatar answered Jan 29 '23 06:01

Pascal MARTIN


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) );
like image 29
Gordon Avatar answered Jan 29 '23 07:01

Gordon