开发者

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

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开发者_Go百科 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.


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


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


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
  }

}


class myExtenstionClass extends SomeClassInLibrary
{

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜