Fixing some methods of my list implementation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Myamo
Forum Newbie
Posts: 3
Joined: Sun Apr 04, 2010 5:45 pm

Fixing some methods of my list implementation

Post by Myamo »

Hello,

I've recently implemented a class which represents a list. Internally, the list is an array.
Most of its methods work correctly. Only two are making me trouble.

The first method is the contains method. It checks if the list contains the element given to it through a parameter.
It returns true, if the list contains the element.
Here is the method:

Code: Select all

	public function contains($element){
		for($i=0;$i<$this->size();$i++){
			if($element == $this->list[i]){
				return true;
			}
		}
	}
The size method works correctly. $this->list is the array which represents the list.
I use this on a site where I type in the element in an input field and then post it.

Then I call following piece of code:

Code: Select all

if($l->contains($_POST['Element'])){
						echo 'Element is in the list';
					}else{
						echo 'Element is not in the list';
					}
However, this always executed the else part so far, even when I inputted an element which is in the list. Only when I left the input field empty the if part was executed.

Any idea on what is wrong?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Fixing some methods of my list implementation

Post by AbraCadaver »

First off, you forgot the $ before the i in , secondly, I would do this:

Code: Select all

public function contains($element){
    return in_array($element, $this->list);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Fixing some methods of my list implementation

Post by requinix »

AbraCadaver wrote:secondly, I would do this:
For that matter, why implement a List class when PHP already has the functionality for every operation?
Homework exercise?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Fixing some methods of my list implementation

Post by AbraCadaver »

tasairis wrote:
AbraCadaver wrote:secondly, I would do this:
For that matter, why implement a List class when PHP already has the functionality for every operation?
Homework exercise?
+1

I was going to say why not just use in_array() instead of making it a method, but some people like to build their own class and wrap some functions. But yes, maybe it's homework :-)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply