Page 1 of 1

Access to undeclared static property: CTextFilter::$code

Posted: Fri Dec 25, 2015 12:10 am
by Goofan
I can't seem to understand why this is happening...

I'm currently using PHP 7.0.0 (recently uppgraded from 5.6)
This is the Code:
CTextFilter.php

Code: Select all

$text =  "hello";
$filters = "markdown";
public function doFilter($text, $filters) {
      // Define all valid filters with their callback function.
      $code = array(
        'bbcode'   => 'bbcode2html',
        'link'     => 'make_clickable',
        'markdown' => 'markdown',
        'nl2br'    => 'nl2br',  
      );
      
      // Make an array of the comma separated string $filters
      $filter = preg_replace('/\s/', '', explode(',', $filters));
      // For each filter, call its function with the $text as parameter.
      foreach($filter AS $func) {
          var_dump($code[$func]);
        if(isset($code[$func])) {
          $text = self::$code[$func]($text);
        } 
        else {
          throw new Exception("The filter '$filters' is not a valid filter string.");
        }
      }
     
      return $text;
    }
This is the Error:
Access to undeclared static property: CTextFilter::$code


I understand it as it wants the $code variable to be static but I can't figure out how to make it just that...
My tries:

Code: Select all

      static $code = array(
        'bbcode'   => 'bbcode2html',
        'link'     => 'make_clickable',
        'markdown' => 'markdown',
        'nl2br'    => 'nl2br',  
      );
Put it into the Class itself... (added public to it aswell and the result I got was: Code snippet 2:

Code: Select all

class CTextFilter {

    /**
     * Call each filter.
     *
     * @param string $text the text to filter.
     * @param string $filters as comma separated list of filter.
     * @return string the formatted text.
     */
      // Define all valid filters with their callback function.
      static $code = array(
        'bbcode'   => 'bbcode2html',
        'link'     => 'make_clickable',
        'markdown' => 'markdown',
        'nl2br'    => 'nl2br',  
      );

Code: Select all

The filter 'markdown' is not a valid filter string.
Please do help me as I can't seem to understand where I went wrong or if this is a change from php 5.6 to 7.0.0

Thanks in advance,
Thomas

Re: Access to undeclared static property: CTextFilter::$code

Posted: Fri Dec 25, 2015 4:07 am
by requinix
Are you trying to modify a file from somewhere? Shouldn't you contact the author first? Or make sure you're using the latest version? I don't know why you want to do whatever you're doing.

Like, I see this one that also has a doFilter() function, but its code is different than what you have there.

Re: Access to undeclared static property: CTextFilter::$code

Posted: Fri Dec 25, 2015 5:57 pm
by Goofan
Yes this is the work of a swedish online course. I just got the outdated version... As you can see from this site (the school page)
http://dbwebb.se/forum/memberlist.php?m ... rofile&u=2

I'm trying to figure out how it works but todo that indepth I really need to get it to work first and I just want to remove the Error...

This is a way much like bbcodes where it translate text to pure text.

It was a script that worked before when I took the class and now it seems not to work because of the static warning:

Code: Select all

Access to undeclared static property: CTextFilter::$code
Regards,
Thomas

P.S. I'm not using the script I'm playing around with it to understand it and because it was part of my schoolwork I got the access and OK from the author (teacher).


Hope this clears it up abit :)

Re: Access to undeclared static property: CTextFilter::$code

Posted: Sat Dec 26, 2015 6:50 am
by requinix
Fair enough.

"self::$code" refers to a static variable on the class named "code". It's not a variable inside a method but defined directly on the class itself. To use this variable you need

Code: Select all

class CTextFilter {

	/* public (default), protected, or private */ static $code = array(
		'bbcode'   => 'bbcode2html',
		'link'     => 'make_clickable',
		'markdown' => 'markdown',
		'nl2br'    => 'nl2br',  
	);
which looks very much like the last bit of code you posted, except yours suggests that the variable was within the doFilter() method.

If you tried to define $code within the method as "static $code = ..." then what you've done is created a static variable inside the method. You use it within the method using just "$code" - same as any other local variable. A static variable has two major differences from a regular variable:
1. Static variables preserve their values across function calls. If you define "static $var=0" (initialized to zero and only happens once) and then "echo ++$var" then you'd see the value go up by one every time you called the method.
2. Static variables apply to every instance of the class, or to a subclass that doesn't override the method. If you created three instances of the class and called the function once on each instance then you'd still see the value increasing.

Re: Access to undeclared static property: CTextFilter::$code

Posted: Sat Dec 26, 2015 2:18 pm
by Goofan
I fixed it as it was something wrong with the actual file... (I had tried just that)
Thank you for your help