dominicd Posted December 30, 2009 Share Posted December 30, 2009 Currently I am trying to rewrite part of plugin that was written using globals. I can not figure out why it used globals to begin with but can someone point me in the right direction? function rewrite_text( $article, $case_sensitive=false ) { global $optindb; global $optout; Quote Link to comment https://forums.phpfreaks.com/topic/186694-rewriting-globals/ Share on other sites More sharing options...
Adam Posted December 30, 2009 Share Posted December 30, 2009 Mmm, direction of what exactly? Quote Link to comment https://forums.phpfreaks.com/topic/186694-rewriting-globals/#findComment-985973 Share on other sites More sharing options...
dominicd Posted December 30, 2009 Author Share Posted December 30, 2009 How to rewrite the globals. Or rather just making them into variables instead of globals. ? Quote Link to comment https://forums.phpfreaks.com/topic/186694-rewriting-globals/#findComment-985989 Share on other sites More sharing options...
Adam Posted December 30, 2009 Share Posted December 30, 2009 Well technically by "global" it means they were created in the global scope, i.e. outside of any class or function. Adding the global keyword allows you to reference that original variable instead of the one that would be created within the scope of your class method / function. To remove the global keyword you'd need to pass those values to the function (or find another way of making them available). Is this a class method? Quote Link to comment https://forums.phpfreaks.com/topic/186694-rewriting-globals/#findComment-986008 Share on other sites More sharing options...
dominicd Posted December 30, 2009 Author Share Posted December 30, 2009 It is a method here is all of the code which includes this page. include ABSPATH."optindb.php"; $optout=ABSPATH."optout.php"; $rows=file($optout); $syn_donot = " "; foreach ($rows as $row_num => $row) { $syn_donot = $syn_donot.$row." "; } function rewrite_text( $article, $case_sensitive=false ) { global $optindb; global $syn_donot; $dorewrite=strpos($article,"<optout>"); if($dorewrite>0) { $article=str_replace("<optout>","",$article); } else { $synlist=$article; $synchar = array("(", ")", "[", "]", "?", ".", ",", "|", "\$", "*", "+", "^", "{", "}"); $synlist=str_replace($synchar," ",$synlist); $synlist=str_replace(" "," ",$synlist); $my_synlist = explode(" ",$synlist); if (sizeof($my_synlist)>0) { for($i=0;$i<sizeof($my_synlist);$i++) { $syn_replace=$my_synlist[$i]; $syn_replace=str_replace(" ","",$syn_replace); $syn_avoid="no"; if($syn_replace!="") { $pos=strpos($syn_donot, $syn_replace); if($pos>0) { $syn_avoid="yes"; } } $syn_replace=" ".$syn_replace." "; $syn_replace_target=$optindb[$syn_replace]; if(($syn_replace!="")&&($syn_replace!=" ")&&($syn_replace_target!="")&&($syn_avoid=="no")) { $article = str_replace($syn_replace,$syn_replace_target,$article); } } } } return $article; } add_filter('the_excerpt', 'rewrite_text', 2); add_filter('the_content', 'rewrite_text', 2); As for why this is even a global for the life of me I can not figure out. Quote Link to comment https://forums.phpfreaks.com/topic/186694-rewriting-globals/#findComment-986016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.