Jump to content

Rewriting a function


ThePsion5

Recommended Posts

Hi,Not too long ago I had created a large set of php objects that performed several tasks together. Several functions were common to otherwise unrelated objects, so I created a few global functions to simplify development, like so:

function _parseURL($URL){//do various things here}

Now I'm extending one of these classes, but I need to alter the previous function a little in order to do what I need to do. Simply replacing the function calls in the extending class isn't a very good solution as this basically forces me to rewrite most of my original class, which i'm hoping to avoid, which leads me to my actual question: can I override the original function in a different file? In an ideal world, i would love to be able to do this:

require_once('Crawler.class.php') //Contains the class I'm extending and the function in questionclass ErrorCrawler extends Crawler{//some additional code}function _parseURL($URL){//does a few things differently here}

I know that this probably isn't possible, but it would be so very convenient if it was. Any idea?

Link to comment
Share on other sites

Yeah, you can do that. You can just redefine the function, or at least I know you can in PHP5.

class SimpleClass{   // member declaration   public $var = 'a default value';   // method declaration   public function displayVar() {	   echo $this->var;   }}class ExtendClass extends SimpleClass{   // Redefine the parent method   function displayVar()   {	   echo "Extending class\n";	   parent::displayVar();   }}

Note that you can still call the parent method in the extending class with the parent keyword.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...