1

I have tried implementing the basic features of ArrayList (Java ) in PHP .The Arraylist should be capable of adding any type of object (generic as in Java) Can anyone give suggestion for improvement in design/implementation. Here is the code

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ArrayList
{

    private $arrVar;

    function  __construct($option)
    {
        $this->arrVar = array();
        array_push($this->arrVar,$option);
    }

  function addValue($option)
  {
      array_push($this->arrVar,$option);
  }

    function getLastValue()
  {
      $arr = array_pop($this->arrVar);
      return $arr;
  }

}



?>
4
  • 2
    Why can't you use normal PHP arrays? They can contain objects of any type without you having to specify what type. Commented Dec 8, 2010 at 16:53
  • need to write some customized functions. Commented Dec 8, 2010 at 16:54
  • 3
    array_pop deletes the last element of the array, but you function says getLastValue() Commented Dec 8, 2010 at 17:01
  • I suspect that the customized functions you want to write could be written using an iterator. See here for an example of using them with an array of objects. Commented Dec 8, 2010 at 17:04

2 Answers 2

2

Your class doesn't really do anything other than restrict the operations I can perform on the array. It's usually best to adopt the idioms common for a particular language, rather than trying to get the language to resemble some other language you prefer.

Sign up to request clarification or add additional context in comments.

1 Comment

Not to mention that it neither follows the naming conventions for ArrayList nor implements a minimal useful subset of its members.
1

Don't know if this helps:

class MyArray {
   private $my_array;

   function  __construct() {
      $this->my_array = array();
   }

   public function setMyArray($value) {
      $this->my_array = $value;
   }

   public function getMyArray() {
      return $this->my_array;
   }

   public function getLastElement() {
      $last_elem = array_reverse($this->getMyArray());
      return $last_elem[0];
   }
}

$myArr = new MyArray();
$a[] = "Hello"; // use this instead of array_push
$a[] = "World";
$myArr->setMyArray($a);
echo "My Array:<pre>".print_r($myArr->getMyArray(),true)."</pre><br />\n";
echo "Last Element: ".$myArr->getLastElement()."<br />\n";

$a[] = "Yet another element";
$myArr->setMyArray($a);
echo "My Array Again:<pre>".print_r($myArr->getMyArray(),true)."</pre><br />\n";
echo "Last Element Again: ".$myArr->getLastElement()."<br />\n";

Output:

My Array:Array
(
    [0] => Hello
    [1] => World
)

Last Element: World

My Array Again:Array
(
    [0] => Hello
    [1] => World
    [2] => Yet another element
)

Last Element Again: Yet another element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.