2

I am trying to extend a class:

class CustomParsedown extends Parsedown {
    protected function blockComment($Line) { return; }
    protected function blockCommentContinue($Line, array $Block) { return; }
    protected function blockHeader($Line) { return; }
    protected function blockSetextHeader($Line, array $Block = NULL) { return; }
}

function markdown($markdown) {
    return CustomParsedown::instance()->setMarkupEscaped(true)->text($markdown);
}

If I run markdown() with markdown from another page, the changes in the code do not go into effect. For example, I can still create a heading. Am I extending the class correctly?

Streetlamp
  • 1,537
  • 2
  • 15
  • 27

1 Answers1

6

It looks like Parsedowns static function instance() is referencing $instance = new self(); which means it will instantiate a new Parsedown class and not your extending class.

Try duplicating their instance method into your class, I've also changed new self into new static.

class CustomParsedown extends Parsedown {
  static function instance($name = 'default')
  {
      if (isset(self::$instances[$name]))
      {
          return self::$instances[$name];
      }
      $instance = new static();
      self::$instances[$name] = $instance;
      return $instance;
  }
  private static $instances = array();
}

https://github.com/erusev/parsedown/blob/master/Parsedown.php


See also New self vs. new static

Community
  • 1
  • 1
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • After amending the code, I get `Cannot access property CustomParsedown::$instances ` – Streetlamp Aug 11 '15 at 01:45
  • ok add `private static $instances = array();` as well to your class – Scuzzy Aug 11 '15 at 01:46
  • 1
    Huh, now I get `Undefined index: name ` on [line 1384](https://github.com/erusev/parsedown/blob/master/Parsedown.php#L1384). Nevermind, that's a bug in my code. – Streetlamp Aug 11 '15 at 01:50
  • 1
    It's quite strange because now that I look a their documentation, you've done exactly what they documented yet it didn't work... https://github.com/erusev/parsedown/wiki/Tutorial:-Create-Extensions – Scuzzy Aug 11 '15 at 02:00
  • Exactly. I was confused as well, especially because there did not seem to be any github issues related to this exact problem. – Streetlamp Aug 11 '15 at 02:02
  • Time to create one :) – Scuzzy Aug 11 '15 at 02:03
  • Go ahead :D Creating an issue (and fix?) will surely help future users. – Streetlamp Aug 11 '15 at 02:04