我正在尝试扩展一个类:
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);
}
Run Code Online (Sandbox Code Playgroud)
如果我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();
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/erusev/parsedown/blob/master/Parsedown.php
另请参阅新自我与新静态