在我的代码中,我使用了所有的类--静态的,比如:Parsedown::text('text'); --如果我尝试像这样使用它,它会给我一个错误消息,“在没有对象上下文的情况下使用$this ",但是我不知道如何像这样使用Parsedown,因为我只能使用它实例化如下:
$Parsedown = new Parsedown();
echo $Parsedown->text('text');函数文本代码
function text($text)
{
# make sure no definitions are set
$this->DefinitionData = array();
# standardize line breaks
$text = str_replace(array("\r\n", "\r"), "\n", $text);
# remove surrounding line breaks
$text = trim($text, "\n");
# split text into lines
$lines = explode("\n", $text);
# iterate through lines to identify blocks
$markup = $this->lines($lines);
# trim line breaks
$markup = trim($markup, "\n");
return $markup;
}如何使用解析静态?
发布于 2022-08-04 08:53:02
您可以在instance()函数中使用Parsedown类静态
echo Parsedown::instance()->text('Text'); 实例函数代码:
static function instance($name = 'default')
{
if (isset(self::$instances[$name]))
{
return self::$instances[$name];
}
$instance = new static();
self::$instances[$name] = $instance;
return $instance;
}发布于 2022-08-02 08:46:02
嗨,如果您想使用静态方法,您应该告诉php,您的方法在方法定义时是静态的,例如,在您的情况下,您可以这样定义您的方法:
public static function text($text)
{
# make sure no definitions are set
self::DefinitionData = array();
# standardize line breaks
$text = str_replace(array("\r\n", "\r"), "\n", $text);
# remove surrounding line breaks
$text = trim($text, "\n");
# split text into lines
$lines = explode("\n", $text);
# iterate through lines to identify blocks
$markup = self::lines($lines);
# trim line breaks
$markup = trim($markup, "\n");
return $markup;
}如您所见,i user Self::而不是$this->,因为在静态函数中,您不能访问初始化数据,您也需要将您的公共变量定义为静态的。
然后你就可以像这样使用你的功能了
Parsedown::text('text');https://stackoverflow.com/questions/73204250
复制相似问题