9 Replies - 651 Views - Last Post: 29 August 2013 - 01:12 PM Rate Topic: -----

#1 Jony Kale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 13

[REVIEW] PHP Map collection class

Posted 26 August 2013 - 06:19 AM

Hello everyone,

I have worked on a map class yesterday,
thought it'd be cool to have one in PHP.

I know PHP might have a class that does the exact same thing, but this one was just a practice.

Java has a Map class, I used it a lot.
I wanted to make something similar in PHP for it.

I know it may not be as useful as in java, because PHP mostly only executes once on the web, but yeah, might be useful sometimes to store data.


Map.class:

<?php
	
	/**
	 * Map
	 *
	 * Collection of items, with a key.
	 * @Author Jony <artemkller@gmail.com>
	 */
	 
	Class Map
	{
		/**
		 * Annonymous Constructor
		 * Sets up the array.
		 */
		 
		function __construct()
		{
			$this->map = array();
		}
		
		/**
		 * Method add
		 * Adds a new item to the collection with a key.
		 *
		 * Note: Key can NOT be numeric!
		 *
		 * @param key		The key of the item.
		 * @param value		The value of the item.
		 * @return void
		 */
		 
		public function add($key, $value)
		{
			$this->map[$key] = $value;
		}
		
		/**
		 * Contains
		 * Checks if an item with specific key exists.
		 *
		 * @param key The key.
		 * @return Boolean
		 */
		 
		public function contains($key)
		{
			if (is_numeric($key))
			{
				$values = array_keys($this->map);
				$key = (isset($values[$key])) ? $values[$key] : null;
			}
			
			return (isset($this->map[$key])) ? true : false;
		}
		
		/**
		 * Method remove
		 * Removes an item from the collection by key.
		 *
		 * @param key	The key of the item.
		 * @return void
		 */
		 
		public function remove($key)
		{
			if (is_numeric($key))
			{
				$values = array_keys($this->map);
				$key = (isset($values[$key])) ? $values[$key] : null;
			}
			
			unset($this->map[$key]);
		}
		
		/**
		 * Method get
		 * Gets an item from the collection by key.
		 *
		 * Note: If entered numeric, it will get the key 
		 * by it's offset position number. Arrays starting from 0.
		 *
		 * @param key The key of the item
		 * @return Array item value (Either String, Integer, Object).
		 */
		 
		public function get($key)
		{
			if (!is_numeric($key))
			{
				return (isset($this->map[$key])) ? $this->map[$key] : null;
			}
			else
			{
				$values = array_values($this->map);
				return (isset($values[$key])) ? $values[$key] : null;
			}
		}
		
		/**
		 * Clear
		 * Clears the array.
		 *
		 * @return void
		 */
		 
		 public function clear()
		 {
			unset($this->map);
			new Map();
		 }
		 
		 /**
		  * Size
		  * Returns the size of an Map collection
		  *
		  * @return Int
		  */
		  
		 public function size()
		 {
			return count($this->map);
		 }
	}
	
?>



Usage:


$object = new Map();
$object->add("hey", 60);
echo $object->get("hey");

// OR

echo $object.get(0); // Get by offset.

/* Objects storing */

	class Data 
	{
		private $x;
		
		function __construct($x)
		{
			$this->x = $x;
		}
		
		public function getX()
		{
			return $this->x;
		}
	}

$object = new Map();
$object->add("data", new Data("lol")); // store object
echo $object->get("data")->getX(); //get object data




What do you think of it?
Are there any improvements needed for it?

Is This A Good Question/Topic? 0
  • +

Replies To: [REVIEW] PHP Map collection class

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,137
  • Joined: 08-June 10

Re: [REVIEW] PHP Map collection class

Posted 26 August 2013 - 06:32 AM

Quote

What do you think of it?

probably much less code if you work with the array directly. mind that PHP does not have to use classes (like Java).
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: [REVIEW] PHP Map collection class

Posted 26 August 2013 - 05:17 PM

The clear() method doesn't make much sense to me. You unset the map property, and then create a new instance of Map and assign it to nothing. Seems to me like that would result in an "undefined property" warning, and even some unexpected behaviour, the next time the Map instance is used, seeing as all the methods are expecting $this->map to be an array.

If you want to clear the map of all values, you want to either unset all the map elements, or just replace the map property with an empty array. The latter would be my choice.

If this was an attempt to call the constructor again after clearing out the map variable, then just do that directly: $this->__construct();.

This post has been edited by Atli: 26 August 2013 - 05:18 PM
Reason for edit:: Clarified.

Was This Post Helpful? 1
  • +
  • -

#4 Jony Kale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 13

Re: [REVIEW] PHP Map collection class

Posted 27 August 2013 - 04:43 AM

Well basically the clear method, I am first un-setting the map array, so it becomes a null, and then I am calling the constructor by a new object of that class, which is basically directed to the constructor which will create a new array anyway.

Of course I can use


$this->map = array();
$this->map = $this->__construct();




Thank you for letting me know, I just forgot about it ^^.
Was This Post Helpful? 0
  • +
  • -

#5 Jony Kale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 13

Re: [REVIEW] PHP Map collection class

Posted 27 August 2013 - 07:48 AM

Actually I just need this line

$this->map = $this->__construct();



Why can't I edit my own post?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: [REVIEW] PHP Map collection class

Posted 27 August 2013 - 10:46 AM

We have post count restricted for new members wither fewer than a certain number of posts. We've had problems in the past with new members removing their posts after they receive help. Stick around, post, have fun and you'll get some editing permissions as you go. :)
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: [REVIEW] PHP Map collection class

Posted 27 August 2013 - 12:32 PM

View PostJony Kale, on 27 August 2013 - 02:48 PM, said:

Actually I just need this line

$this->map = $this->__construct();


Setting the $this->map property like that is not a good move there. The constructor will return a null, so that line calls the constructur, which initializes the map, but then you overwrite that by assigning it the returned null. The end result will always be a null for the map, regardless of what the constructor does.

All you need there is to call the constructor. Nothing else is required. It will initialize map back to an empty array just like it did when the class was first created.
public function clear()
{
    $this->__construct();
}


Was This Post Helpful? 2
  • +
  • -

#8 Jony Kale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 13

Re: [REVIEW] PHP Map collection class

Posted 28 August 2013 - 08:48 AM

Very true, sorry I didn't notice that ^^/>.

This post has been edited by Dormilich: 28 August 2013 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: [REVIEW] PHP Map collection class

Posted 28 August 2013 - 07:50 PM

Im sorry, I understand you did this for practice, but I am very confused of how is this ever going to be useful in php? The way I see it, its a basic php array functionality. I simply want to know what you were thinking at the time. I feel like I have missed an important point here. X.X

Im not posting this to pick on you, simply to satisfy my own curiosity.

Oh and another thing, Why are you checking if the key is numeric in contains and remove functions? PHP arrays can have both numeric and named keys at the same time. I'm like staring at those few lines and I just dont see it x.x Unless.... its like a number of the element in the collection where you sequentially would find the element? Like idc what key you got, but gimme the 3rd element?

Also the clear() function seems meaningless to me.(though it makes sense to have a function like that) If I wanted to clear it, wouldn't I simply create a new instance in my code overriding the old one?

As for a practice run in creating a data structure, you possibly could have added some functions like, nextObject and previousObject. This would also require a pointer to be added. Adding some things like that would perhaps make it more useful as a whole. (oh god its been forever since I touched Data structures)

Other than that, I think its a solid attempt. Keep at it ;) One thing a lot of people dont understand about programming is that its a constant practice. Its very pleasing to see people that do small things like this to make them selves better. Very nice thing to see indeed ^^
Was This Post Helpful? 1
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: [REVIEW] PHP Map collection class

Posted 29 August 2013 - 01:12 PM

View PostAtspulgs, on 29 August 2013 - 02:50 AM, said:

Oh and another thing, Why are you checking if the key is numeric in contains and remove functions?

You should read the doc-blocks above the add and get functions. They explain this.

Basically, this Map class doesn't follow PHP's normal behaviour of allowing numeric keys. Instead it uses numbers as offsets, to fetch the array element at that position in the map, regardless of the key values.
$map = new Map();
$map->add("first", 1);
$map->add("second", 2);
$map->add("third", 3);

var_dump($map->get(1)); // 2



Now that you mention it, though, it occurs to me that the add method should enforce this restriction, so that you don't end up accidentally using numbers out of habit, and having it lead to bugs.


It would also be a good idea to replace the is_numeric calls in remove, contains and get with a type-specific check, like is_int, to avoid situations where strings would evaluate to numbers, thus failing to produce the right value when called.
$phoneList = new Map();
$phoneList->add("5332000", "Pizza Hut");
$phoneList->add("5812345", "Domino's Pizza");

var_dump($phoneList->get("5812345")); // Null


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1