繁体   English   中英

扩展PHP中的类

[英]Extend class in PHP

我正在尝试扩展课程:

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

如果我从另一页运行带有markdown的markdown() ,则代码中的更改不会生效。 例如,我仍然可以创建标题。 我可以正确地上课吗?

看起来Parsedowns static function instance()引用$instance = new self(); 这意味着它将实例化一个新的Parsedown类,而不是您的扩展类。

尝试将其实例方法复制到您的类中,我也将new self更改为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


另请参阅新自我与新静态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM