Page 1 of 1

Using $this when not in object context in

Posted: Fri Apr 29, 2016 9:14 am
by jshadow
Thia code works fine on the php 4 server. But now that I've moved to a php 5 server my website don't work
and the server returns: "Using $this when not in object context in ..." if ($this->nofollow) {

Code: Select all

class plgContentAllWeblinks  extends JPlugin
{
        // plugin parameters 
        // _plgContentAllWeblinksReplace will set the values;
        protected $days_new;
        protected $txt_new;
        protected $days_mod;
        protected $txt_mod;
        protected $lengthoftitle;
        protected $orderby;
        protected $Corderby;
        protected $linkcount;
        protected $displayemptycat;
        // These params can also be overridden in the commmandline
        protected $real_url;
        protected $show_title;
        protected $show_new;
        protected $show_mod;
        protected $show_hits;
        
        protected $new_window;
        protected $moduleclass_sfx;     
        protected $num_cols;    
        protected $layout;      
        protected $show_header; 
        protected $show_cdate;  
        protected $show_mdate;
        protected $show_author;
        protected $display_cdescription;
        protected $display_ldescription;        
        protected $DEBUG = 0;   
        protected $exclude_id = 0;	
        function plgContentAllWeblinks( &$subject, $params )	{	
            parent::__construct( $subject, $params );	
            
        }       /**        * Plugin that loads module positions within content        */	// onPrepareContent, meaning the plugin is rendered at the first stage in preparing content for output	public function onContentPrepare($context, &$article, &$params, $limitstart)	{				// simple performance check to determine whether bot should process further		if ( JString::strpos( $article->text, 'all_weblinks' ) === false ) {				return true;		}					// Don't repeat the CSS for each instance of this bot in a page!					static $included_allweblinks_css;										if (!$included_allweblinks_css) {						$document = JFactory::getDocument();						$url = 'plugins/content/allweblinks/css/allweblinks.css';						$document->addStyleSheet($url);							$document->addCustomTag( '<!--[if lte IE 6]>						<style type="text/css">													.tooltip:hover span{								left: auto;							}						   							.tooltip span{           								left: -9999px;   							}						   						</style>						<![endif]-->' );							$included_allweblinks_css = 1;					}		global $mainframe;		// A database connection is created			$db = JFactory::getDBO();			// expression to search for			$regex = "#{all_weblinks\s*(.*?)}(.*?){/all_weblinks}#s";			// Get plugin info			// check whether plugin has been unpublished			if ( !$this->params->get( 'enabled', 1 ) ) {					$article->text = preg_replace( $regex, '', $article->text );					return true;			}			// find all instances of plugin and put in $matches			preg_match_all( $regex, $article->text, $matches );			// Number of plugins			$count = count( $matches[0] );			// plugin only processes if there are any instances of the plugin in the text			if ( $count ) {					$this->_process( $article, $matches, $count, $regex, $params);			}			// No return value	}	// The proccessing function	protected function _process( &$article, &$matches, $count, $regex, $params )	{		// Get the links for every occurence  of all_weblinks		for ( $i=0; $i < $count; $i++ )		{				$load = str_replace( 'all_weblinks', '', $matches[0][$i] );				$load = str_replace( '{', '', $load );				$load = str_replace( '}', '', $load );				$load = trim( $load );				$this->_plgContentAllWeblinksReplace($article, $matches, $i, $count, $regex, $params ) ;	 		}	}	protected function _plgContentAllWeblinksReplace(&$article, &$matches, $i, $count, $regex, $params) 	{		// Get plugin parameters		$param_line   = $matches[1][$i];		$catids       = trim($matches[2][$i]);		$this->days_new = $this->params->def('Ldaysnew');		$this->txt_new = $this->params->def('Ltxtnew');		$this->days_mod = $this->params->def('Ldaysmod');		$this->txt_mod = $this->params->def('Ltxtmod');		$this->lengthoftitle = $this->params->def('Llengthoftitle', 0);		$this->orderby 	  = $this->params->def( 'Lorder', 'date DESC');		$this->Corderby     = $this->params->def( 'LCorder', 'a.catid');		$this->linkcount    = $this->params->def( 'Lcatlinks', 999);				$this->displayemptycat    = $this->params->def( 'Ldisplayemptycat', 0);				// These params can also be overridden in the commmandline		$this->real_url     = $this->_params($param_line, 'real_url', $this->params->def('Lrealurl'));		$this->show_title   = $this->_params($param_line, 'show_title', $this->params->def('Lshowtitle'));		$this->show_new     = $this->_params($param_line, 'show_new', $this->params->def('Lshownew'));		$this->show_mod     = $this->_params($param_line, 'show_mod', $this->params->def('Lshowmod'));		$this->show_hits    = $this->_params($param_line, 'show_hits', $this->params->def('Lshowhits'));		$this->new_window   = $this->_params($param_line, 'new_window', $this->params->def('Lpopuplinks'));		$this->nofollow     = $this->_params($param_line, 'nofollow', $this->params->def('Lnofollowlinks'));		$this->moduleclass_sfx = $this->_params($param_line, 'moduleclass_sfx', $this->params->def('moduleclass_sfx'));		$this->num_cols     = $this->_params($param_line, 'num_cols', $this->params->def('Lnumcols'));		$this->layout       = $this->_params($param_line, 'layout', $this->params->def('Llayout'));		$this->closepanes   = $this->_params($param_line, 'closepanes', $this->params->def('Lclosepanes'));		$this->show_header  = $this->_params($param_line, 'show_header',$this->params->def('Lshowheader'));		$this->show_cdate  = $this->_params($param_line, 'show_cdate',$this->params->def('Lshowcdate'));		$this->show_mdate  = $this->_params($param_line, 'show_mdate',$this->params->def('Lshowmdate'));		$this->show_author  = $this->_params($param_line, 'show_author',$this->params->def('Lshowauthor'));		$this->display_cdescription  = $this->_params($param_line, 'display_cdescription',$this->params->def('Ldisplaycdescription'));		$this->display_ldescription  = $this->_params($param_line, 'display_ldescription',$this->params->def('Ldisplayldescription'));		// These params can ONLY be set in the commmandline				$this->DEBUG        = $this->_params($param_line, 'DEBUG', '0');		$this->exclude_id   = $this->_params($param_line, 'exclude_id', '0');		//$this->DEBUG=1;		switch ($this->layout)		{			case 0 :				$llayout="";				break;			case 1 :				$llayout="_col";				break;			case 2 :				$llayout="_blocks";				break;			case 3 :				$llayout="tabs";				break;			case 4 :				$llayout="sliders";				break;			default: // if a wrong layout is choosen reset to 0				$llayout="";								$this->layout=0;		}		$replace ="";
        }
				// count Categories		!isset($GLOBALS['all_weblinks']) ? $GLOBALS['all_weblinks'] = 1 : $GLOBALS['all_weblinks']++;		# DEBUG if requested by specifing DEBUG=[1]		if ($this->DEBUG)	$replace .= "<div class=\"debug\">### DEBUG plugin-parameters: 		<ul>		<li>days_new=[$this->days_new]</li>		<li>txt_new=[$this->txt_new]</li>		<li>show_title=[$this->show_title]</li>		<li>show_new=[$this->show_new]</li>		<li>days_mod=[$this->days_mod]</li>		<li>txt_mod=[$this->txt_mod]</li>		<li>show_mod=[$this->show_mod]</li>		<li>displayemptycat=[$this->displayemptycat]</li>		<li>new_window=[$this->new_window]</li>		<li>nofollow=[$this->nofollow]</li>		<li>show_hits=[$this->show_hits]</li>		<li>show_header=[$this->show_header]</li>		<li>show_cdate=[$this->show_cdate]</li>		<li>show_mdate=[$this->show_mdate]</li>		<li>show_author=[$this->show_author]</li>		<li>lengthoftitle=[$this->lengthoftitle]</li>		<li>display_cdescription=[$this->display_cdescription]</li>		<li>display_ldescription=[$this->display_ldescription]</li>		<li>number of columns=[$this->num_cols]</li>		<li>orderby=[$this->orderby]</li>		<li>Corderby=[$this->Corderby]</li>		<li>linkcount=[$this->linkcount]</li>		<li>exclude_id=[$this->exclude_id]</li>		<li>catids=[$catids]</li>		<li>layout=[$this->layout]</li>		<li>closepanes=[$this->closepanes]</li>		</ul>		### DEBUG END ###</div>";						# Initialize and clear the content from other modules		$cs=0;		$tarticle=0;		$Itemid=1;		if ($this->moduleclass_sfx=='' && $this->layout<3) $this->moduleclass_sfx = $llayout;				// Start of weblinks output		if ($this->layout>=3) {  // Slider or Tabs 			if  ($this->closepanes) {				$pane =& JPane::getInstance($llayout, array('startOffset'=>-1));			} else {				$pane =& JPane::getInstance($llayout,0);			}			    $replace      .= $pane->startPane( 'pane' );			$linktag = "p";			// set defaults for number of columns and always show description and don't show header			$this->show_header=0;			$this->num_cols=1;			$this->display_cdescription=1;			$this->display_ldescription=1;		} else {			$replace      .= "<div id=\"allweblinksbox".$this->moduleclass_sfx."\">";			$linktag="li";		}		if ($this->show_title==0) $this->display_ldescription = 1; // no title, just description						# Put the output on screen		$colnum=0;		// Get the categories		$clist = $this->_getCatLinks($catids);				// for every category we work		foreach ($clist as $dbcategory) {			/* display empty categories? */			if  (!$this->displayemptycat && $dbcategory->numlinks==0) continue; 			$colnum++;			if ($colnum > $this->num_cols) $colnum=1;						if ($this->layout<3) $replace .= "<div class=\"allweblinks".$this->moduleclass_sfx." cols". $this->num_cols." c".$colnum."\">";			if ($this->show_header)  $replace .= "<h2 class=\"cat_title".$this->moduleclass_sfx." c".$colnum."\">". $dbcategory->ctitle  ."</h2>";			// Start category			if ($this->layout>=3) {			    $replace .= $pane->startPanel(strip_tags($dbcategory->ctitle), "cat" . $dbcategory->catid);			} else { 				$replace .= "<ul class=\"links".$this->moduleclass_sfx."\">"; /* place categorytitle */			}			if ($this->display_cdescription) $replace .= "<p>". ($dbcategory->cdescription) ."</p>"; /* place category description */			//Process the links				$replace .= $this->_processLinks($this->_getLinks($dbcategory->catid),$linktag);						// end of category			if  ($dbcategory->numlinks > $this->linkcount) 			{				$CURL = JRoute::_(WeblinksHelperRoute::getCategoryRoute($dbcategory->catid));			    $nlinks = $dbcategory->numlinks - $this->linkcount;				$replace.= "<p class=\"readmore\"><a href=\"".	$CURL ."\">more links[$nlinks]</a></p>";			}			if ($this->layout>=3) 			{			    $replace .= $pane->endPanel();			} 			else 			{ 				$replace .= "</ul></div>\n";			} 		}		if ($this->layout>=3) 		{			$replace .= $pane->endPane();		} 		else 		{ 			$replace .= "</div>\n";		}		$replace .="<!-- Created by SJL Creations, see more on http://www.sjlcreations.be -->";	  		$article->text = str_replace($matches[0][$i], $replace, $article->text);	}		private function _params( $wl_params, $param, $default = false ){		// Regular expression to find the param from string $wl_params		$regex = "/". $param ."=(\s*\[.*?\])/s";		// Put params into the variable $options		preg_match_all( $regex, $wl_params, $options );		// If there is an option found remove any whitespace and return, if none are found return default value		$value = !empty($options[1][0]) ? JString::trim( $options[1][0], '[]' ) : trim($default);		// Return the value		return $value;	}	protected function _processLinks ($list, $linktag) 	{		$tarticle = 0;
		$replace = "";
		// for every article we work		foreach ($list as $dbarticle) {			if ($this->show_title) {				// Set the length of the title				if( (strlen($dbarticle->title) > $this->lengthoftitle) && $this->lengthoftitle > 0 )				{					$dotaddlenght = $this->lengthoftitle-3;					$dbarticle->title = utf8_substr(strip_tags($dbarticle->title),0,$dotaddlenght); 					$dbarticle->title .= "...";				}						$tarticle+=1;				/* Calcuate new or modified*/				$today = getdate();				$new_date = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-$this->days_new, date("Y")));				$mod_date = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-$this->days_mod, date("Y")));				$rdate = $dbarticle->cdate; //Creation date				$rmdate = $dbarticle->mdate; // Modified date				if ($rdate > $new_date) { $new = " <span class=\"new".$this->moduleclass_sfx."\">$this->txt_new</span>"; } else { $new=""; }				if ($rmdate > $mod_date) { $mod = " <span class=\"mod".$this->moduleclass_sfx."\">$this->txt_mod</span>"; } else { $mod=""; }				/* end new/modified */								if ("$dbarticle->description"=="") {					$title=strip_tags($dbarticle->title);				} else {					$title=strip_tags($dbarticle->description);				}								//link building			//	$URL = JRoute::_(WeblinksHelperRoute::getWeblinkRoute($dbarticle->catid, $dbarticle->id));			//	$URL=JRoute::_("index.php?option=com_weblinks&view=weblink&catid=".$dbarticle->catslug."&id=".$dbarticle->slug);				$URL=JRoute::_("index.php?option=com_weblinks&task=weblink.go&id=".$dbarticle->id);				if ($this->real_url) $URL=$dbarticle->siteurl;				if ($this->new_window) {$target="target='_blank'"; } else {$target="";};
			        if ($this->nofollow) {
                                    $nofollow=" rel=\"nofollow\" "; 
                                    
                                } else {
                                    $nofollow="";
                                    
                                };
The problematic line is: if ($this->nofollow) {

Re: Using $this when not in object context in

Posted: Fri Apr 29, 2016 9:37 am
by thinsoldier
PHP 4 had inferior, overly lenient, incompletely implemented Object Oriented Programming features.

PHP 5 has better and stricter OOP features.

You must learn the basics of OOP and update your code.

PHP4 died almost 8 years ago: http://php.net/eol.php

Please paste your code somewhere like pastebin or a github gist so it can be properly formatted and have line numbers and then point to the line number with the problem.

Re: Using $this when not in object context in

Posted: Fri Apr 29, 2016 10:27 am
by thinsoldier
My best guess is you have a typo regarding the placement of a "}" to close a class method. When I tried to format your code soup, somewhere shortly after switch ($this->layout) the function seems to have ended via "}" prematurely leaving the rest of the code ouside of the class and thus disconnected from any $this that would make sense.

Re: Using $this when not in object context in

Posted: Fri Apr 29, 2016 2:27 pm
by Christopher
This may be the same problem you had before. The // comment directs the parser to ignore all text until the end of the line. Because you formatting is messed up, many valid lines of code are commented out in the code above. Fix the formatting and the code will run fine.

If that is not the problem, a difference between PHP4 and PHP5 is that the constructor function had the same name as the class before PHP5 (e.g. plgContentAllWeblinks() ), but in PHP5 it is named __construct(). You code may be referring to the plgContentAllWeblinks() method directly.