Reputation: 45
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 ?
Upvotes: 0
Views: 139
Reputation: 45
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;
}
Upvotes: 0
Reputation: 327
Hi if you want to use a method static you should tell php you method was static At method definition for example in you case you can define you method like this :
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;
}
as you can see i user Self::
instead of $this->
because in static function you cannot access to initialize data you need to define your public variable as static too
then you can use you function like this
Parsedown::text('text');
Upvotes: 1