phpparsedown

Use parsedown classes static


On my code I use all my classes static like: Parsedown::text('text'); and if I try to use it like that it gives me an error message "Using $this when not in object context ", but I can't figure out how to use Parsedown like this cause I can only use it instantiated like:

$Parsedown = new Parsedown();
echo $Parsedown->text('text');

Code for function 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;
    }

How can I use parsedown static ?


Solution

  • You can use Parsedown class static with instance() function

    echo Parsedown::instance()->text('Text'); 
    

    Instance function code :

        static function instance($name = 'default')
        {
            if (isset(self::$instances[$name]))
            {
                return self::$instances[$name];
            }
    
            $instance = new static();
    
            self::$instances[$name] = $instance;
    
            return $instance;
        }