[英]Extend class in PHP
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. 如果我从另一页运行带有markdown的
markdown()
,则代码中的更改不会生效。 For example, I can still create a heading. 例如,我仍然可以创建标题。 Am I extending the class correctly?
我可以正确地上课吗?
It looks like Parsedowns static function instance()
is referencing $instance = new self();
看起来Parsedowns
static function instance()
引用$instance = new self();
which means it will instantiate a new Parsedown
class and not your extending class. 这意味着它将实例化一个新的
Parsedown
类,而不是您的扩展类。
Try duplicating their instance method into your class, I've also changed new self
into new static
. 尝试将其实例方法复制到您的类中,我也将
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 https://github.com/erusev/parsedown/blob/master/Parsedown.php
See also New self vs. new static 另请参阅新自我与新静态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.