php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16947 array issue in object
Submitted: 2002-05-01 12:22 UTC Modified: 2002-05-02 08:55 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: max at reallyusefulcomputing dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.1.2 OS: SunOS 5.8 Generic_108528-
Private report: No CVE-ID: None
 [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.



Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-01 14:04 UTC] msopacua at idg dot nl
So what are you trying to do in those lines?

$this->$violations expands to $this->array(.....).
You want to make assign several properties all at once?
Or is:
$this->violations=$arr_violations;

the code you're looking for? And if so, does that fix the bug?
 [2002-05-01 14:34 UTC] max at reallyusefulcomputing dot com
i am trying to set the object variables $violations and $keywords to be $violation_arr and $keyword_arr respectively. the assignment itself works however it assigns the last value in this case $keyword_arr to both. if i invert the order of the assignments doing the $violation assignment last then both are set to that value. 

i do not believe the problem has anything to do with my use of this-> because again a value is being assigned but it is assigning to more than one variable. 

of additonal note it does not set any of the other variables $filter_name, $allowed_number and $filter_word_file.
 [2002-05-01 15:03 UTC] msopacua at idg dot nl
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.
 [2002-05-01 15:24 UTC] max at reallyusefulcomputing dot com
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...
 [2002-05-01 15:34 UTC] max at reallyusefulcomputing dot com
checked the os with phpinfo()
 [2002-05-02 08:55 UTC] sniper@php.net
The bug system is not the appropriate forum for asking support
questions. For a list of a range of more appropriate places to ask
for help using PHP, please visit http://www.php.net/support.php


 [2002-05-02 11:06 UTC] max at reallyusefulcomputing dot com
>The bug system is not the appropriate forum for asking support
>questions. For a list of a range of more appropriate places to ask
>for help using PHP, please visit http://www.php.net/support.php

fuck off
 [2002-05-02 11:06 UTC] max at reallyusefulcomputing dot com
>The bug system is not the appropriate forum for asking support
>questions. For a list of a range of more appropriate places to ask
>for help using PHP, please visit http://www.php.net/support.php

fuck off
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Aug 18 20:01:29 2024 UTC