0

I tried a lot of search but unable to figure out why array $wordlinks in function DoWordLink is not carrying values from function __construct. PHP class code as below:

<?php

class autolinkkeyword
{

    public $wordlinks = array();

    public function __construct(){
        $query = mysql_query("SELECT keyword FROM library");
                while ($row = mysql_fetch_array($query))
                {
                    $this->wordlinks [$row["keyword"]] = $row["keyword"];
                }
    }               

    public function linkkeywords ($posts)
    {                
            function DoWordLink($match)
            {
                $rpl=$match[1];
                if(isset($wordlinks[$rpl])) 
                {
                        $kword = $this->wordlinks[$rpl];
                        $rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv'); 
                                ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
                        unset($this->wordlinks[$match[1]]);
                }
                return $rpl;
            }

            $wl=array_keys($this->wordlinks);           
            $pm="/((?<=\s|^)(?:" . implode('|',$wl) .")(?=\.|\!|\?|\,|\'|\s|$))/im";
            foreach($posts as $key => $mainbody)
            {
                $mainbody=preg_replace_callback($pm, 'DoWordLink', $mainbody)  ;    
                echo $mainbody;
            }

    }       
}
?>
2
  • 3
    isset($wordlinks[$rpl]) should be isset($this->wordlinks[$rpl]) Commented Jun 21, 2011 at 20:51
  • 4
    Nested functions for PHP is not good guy. Commented Jun 21, 2011 at 20:52

4 Answers 4

1

You can make it an actual method of that class and call it using this method: http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback like:

preg_replace_callback($pm, array($this, 'DoWordLink'), $mainbody);

Change DoWordLink function so it is part of the class like:

class autolinkkeyword
{
  function DoWordLink($match)
  {
    $rpl=$match[1];
    if(isset($this->wordlinks[$rpl])) 
    {
      $kword = $this->wordlinks[$rpl];
      $rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv'); 
      ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
      unset($this->wordlinks[$match[1]]);
    }
    return $rpl;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

aren't you missing a "this->" construct here? if(isset($this->wordlinks[$rpl]))

Comments

0

Use the $this everywhere you refer to $wordlinks.

$this->wordlinks

Comments

0

You need to access the property in your linkkeywords-method with the object-accessor, too!

public function linkkeywords ($posts)
{

   // Here use $this->wordlinks not $wordlinks
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.