开发者

Given a PHP class would be the best and simplest way to override one or two of its methods with one of your own?

Here's the objective. I have a PHP class and there are one or two of its methods that I would like to override with my own. As I understand OOP (in PHP and in general) I could wri开发者_开发技巧te a child class that extends it and overrides the functionality of the methods in question.

However, I was wondering if this is the best way of achieving this task and if this is a proper use for child classes or if there is something better in PHP for what I'm trying to do.


Yes, that is the best way to do it. The idea of extending a class is to provide extra or more specific functionality.


yes. this exactly what you use inheritance for.

A good principle though, is make sure your new inheriting class "is-a" base class.

Like Human is-a Mammal. Don't do Alien extends Human just because Alien does a lot of stuff Humans do.


So, are you asking if overriding methods (which requires extending the superclass) is the best way to override methods? Yes.

That is the most important use of subclasses. In fact, polymorphism is at the heart of OOP. The other use is to provide new properties/methods, but that usually won't be enough.


class oldClass {
  public function methodToOverride() {
    echo 'oh hai';
  }
}

class childClass extends oldClass {
  public function methodToOverride($arg, $arg2) {
    // your custom code
    echo 'hai world ' . $arg . ' ' . $arg2;

    // if you need to still call the parent class
    parent::methodToOverride();
  }
}

$child = new childClass();
$child->methodToOverride('nom', 'nom');


Sounds like you're on the right track.

For the methods you want to override, you'd probably want to:

  • make sure you're not changing the intent of the method
  • know whether you have to call the base class's method within, and when


The only other way, would be to create a generic "superclass"...

class SuperClass {
    protected $obj = null;
    protected $overrides = array();

    public function __construct($obj) {
        if (!is_object($obj)) {
            throw new InvalidArgumentException('Argument is not an object');
        }
        $this->obj = $obj;
    }

    public function __call($method, $args) {
        $method = strtolower($method);
        if (isset($this->overrides[$method])) {
            array_unshift($args, $this);
            return call_user_func_array($this->overrides[$method], $args);
        } elseif (is_callable(array($this->obj, $method))) {
            return call_user_func_array(array($this->obj, $method), $args);
        } else {
            throw new BadMethodCallException('Invalid Method Called');
        }
    }

    public function __get($var) {
        return isset($this->obj->$var) ? $this->obj->$var : null;
    }

    public function __set($var, $value) {
        $this->obj->$var = $value;
    }

    public function addOverride($method, $callback) {
        $this->overrides[strtolower($method)] = $callback;
    }
}

It's not always the best solution, but it's possible that some situations exist to use something like that. It will let you "add" and "override" methods to any object at run time.

The better generic solution is to simply extend the class in a child class... But the above "superclass" does have some uses...


Another route I haven't seen mentioned here is to question whether it wouldn't be better to extract an abstract class and have the two classes extend that. Of course you would need to be able to alter the code of the first class for that (e.i. if the class came from an open source library you might refrain from changing the code).


you are on the right track

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜