Re: PHP5 oop question...

From: Date: Mon, 28 May 2007 10:47:50 +0000
Subject: Re: PHP5 oop question...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-255705@lists.php.net to get a copy of this message
Andrei wrote:
    Hi list,
    I have a class which I use to parse simple bbcode inside some comments.
    I noticed on PHP5 that scope of preg_replace function is changed
when function is called inside a class. To the point: [CODE] class PHS_editor {
    ...
    function parse_content( $str = null )
    {
        $from_arr = array( "@\[B\](.*?)\[\/B\]@sim",
"@\[U\](.*?)\[\/U\]@sim", "@\[I\](.*?)\[\/I\]@sim",
                           "@\[URL=([^\]]*)\]([^\[]*?)\[\/URL\]@sim",
                           "@\[IMG=([^\]]*)\]@semi",
                           "@\[QUOTE=([^\]]*)\]([^\[]*?)\[\/QUOTE\]@semi"
                            );
        $to_arr = array( '<b>\1</b>', '<u>\1</u>', '<i>\1</i>',
                         '<a href="\1" target="_blank">\2</a>',
                         "'<img src=\"'.stripslashes(
\*$this->get_image_location*( '\\1' ) ).'\" border=\"0\">'",
                         "'<table width=\"98%\" align=\"center\"
cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"form_type\">".
                          "<tr>".
                            "<td class=\"maintext\">'.stripslashes(
\*$this->remove_mytags*( '\\2' ) ).'</td>".
                         "</tr>".
                         "</table>'"
                         );
        if( is_null( $str ) )
            $str = $this->editor_content;
               return preg_replace( $from_arr, $to_arr, str_replace( "  ", "
&nbsp;", nl2br( $str ) ) );
    }
    ...
} [/CODE]
    When it gets to parse [IMG] tags I get "Fatal error: Using $this
when not in object context in ...". So it seems they changed the scope for preg_replace callback functions. As this function is called inside the method shouldn't it have the scope of the class?
    Is there a workaround for this? I cannot declare a function
get_image_location which will staticly call the method bcuz I use variables from instanced class.
How are you trying to use this class? It sounds like you're trying to use the method statically. When a method is called statically it does not have a $this. -Stut

« previous php.general (#255705) next »