|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-05-01 12:22 UTC] max at reallyusefulcomputing dot com
i have created a class called filter. i have two variables in the class which are arrays. i have an method named approve which sets the values of both arrays. however the arrays both get set to the same value.
here is the relevant portion of the filter class code.
class Filter{
var $filter_name = "Basic Filter";
var $allowed_number = 0;
var $filter_word_file; // file name string
var $violations; // array of strings of violating keywords
var $keywords; // array of strings of keywords in content
// Basic filter constructor.
// Returns : nothing
// Parameters : $file_name File containing restricted words.
function Filter($file_name){
$this->filter_word_file=$file_name;
}
// Compares passed content to restricted file. Approved if unwanted content falls
// in range specified by $violation_count.
// Returns : true if approved
// false if not approved
// Parameters : $content the content to test
function approve($content){
$word_arr = file($this->filter_word_file);
$content_arr = split(" ",$content);
$violation_count = 0;
$violation_arr;
$keyword_arr;
$run_once = true;
for($i=0;$i<count($word_arr);$i++){
for($j=0;$j<count($content_arr);$j++){
if(ereg(trim($word_arr[$i]),$content_arr[$j])){
$violation_arr[$violation_count] = $content_arr[$j];
$violation_count++;
}else{
if((strlen($content_arr[$j])>4)&& ($run_once == true)){
$keyword_arr[] = $content_arr[$j];
}
}
}
$run_once = false;
}
//***** THIS IS THE CODE THAT IS BUGGY!!!! ******
$this->$violations = $violation_arr;
$this->$keywords = $keyword_arr;
//***** NOW HERE BOTH $keywords and $violations ARE SET TO THE VALUE OF $keyword_arr *****
if($violation_count>$allowed_number){
return false;
}
return true;
}
// Sets the number of allowed violations.
// Return : nothing
// Parameters : $number Amount of violations to allow.
function setAllowedViolationNumber($number){
$this->$allowed_number = $number;
}
// Returns an array of content violations.
// Return : array of strings
// Parameters : none
function getViolatingContent(){
return $this->$violations;
}
// Returns an array of keywords for content.
// Return : array of strings
// Parameters : none
function getKeywords(){
echo count($keywords);
return $this->$keywords;
}
}
Anyway I can always re-produce this bug and I think there is something seriously wrong here.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 17:00:01 2025 UTC |
Looking at your code, your assigning $this->$violations which is very different from $this->violations (NOTICE: the dollar sign AFTER the arrow). At the time of assignment, the $violations, is an empty var even. So what would happen is that $this->=$arr_violations. If that does anything usefull, it would probably be, something like: foreach($violations as $key => $value) { $this->$key=$value; } The object would then get property '0', '1','2' etc. But that is more speculation than fact, because what you're trying to do here is incorrect. use: $this->violations=$arr_violations; then report back.three test results... ----test case html---- <html> <head> <title>Filter Test</title> </head> <body bgcolor="#ffffff"> <?php $input_to_test = "Here are some words. here is a naughty words. Some final ending content."; require("includes/old_filter.php"); $my_filter = new Filter("word_files/basicwords.txt"); $my_filter -> approve($input_to_test); $v = $my_filter ->getViolatingContent(); echo "<H4>Violating</H4>"; for($i=0;$i<count($v);$i++){ printf("%s<br>",$v[$i]); } echo "<h4>Keywords</h4>"; $keywords = $my_filter ->getKeywords($input_to_test); for($i=0;$i<count($keywords);$i++){ printf("%s<br>",$keywords[$i]); } ?> </body> </html> TEST #1 ---- changing code as you suggested ----- $this->violations = $violation_arr; $this->keywords = $keyword_arr; produces the following output Violating Keywords 0 TEST #2 ---- my posted code----- $this->$violations = $violation_arr; $this->$keywords = $keyword_arr; produces the following output Violating words. naughty words. final ending content. Keywords 0words. naughty words. final ending content. TEST #3 ---- using my code but inverting the two assignment statements----- $this->$keywords = $keyword_arr; $this->$violations = $violation_arr; produces the following output Violating naughty Keywords 0naughty again i remain pretty convinced this is some sort of bug...