Hi All,
I was just messing around with arrays and objects. I built these two classes based on Java's Stack and Vector Classes. The Vector class is not a full conversion, but more of a start. Also this is not a true Vector because it is does not resize on demand like a Java Vector. I just wanted your thoughts on the classes if any. Thanks in advance.
One question, this might just be preference but, I only allow the storage of other objects. Do you think that this would be more useful if I allowed the storage of Integers or Strings or would these be seperate Objects?
Stack Class:
Vector Class:PHP Code:<?php
require_once('Vector.class.php');
class Stack extends Vector
{
//CONSTRUCTOR
/***
* <p>Creates an empty Stack.</p>
* @returns void
***/
function Stack()
{
$this->Vector();
}
//END CONSTRUCTOR
//MANIPULATORS
/***
* <p>Pushes an item onto the top of this stack.</p>
* @param $o Object to push onto the Stack
* @returns int 1 on success and int 0 on failure
***/
function push(&$o)
{
$blnPushed = 0;
if(is_object($o))
{
$this->arrObjects[] =& $o;
$blnPushed = 1;
}
return $blnPushed;
}
/***
* <p>Removes the object at the top of this stack and returns that
* object as the value of this function. </p>
* @param $o Object to pop off of the end of the Stack
* @returns obj Object on success and int 0 on failure
***/
function pop()
{
$o = 0;
if(!$this->isEmpty())
{
$o = array_pop($this->_getObjects());
}
return $o;
}
/***
* <p>Looks at the object at the top of this stack without removing it from the stack.</p>
* @param $o Object to peek at in the Stack
* @returns obj Object on success and int 0 on failure
***/
function peek()
{
$o = 0;
if(!$this->isEmpty())
{
$lastElement = $this->size() - 1;
$o = $this->arrObjects[$lastElement];
}
return $o;
}
/***
* <p>Returns the 1-based position where an object is in the Stack.</p>
* @param $str String
* @returns $intPostition Integer on success and -1 if the item is not in the Stack
***/
function search($str)
{
$intPostition = -1;
$arrTemp =& $this->_getObjects();
for($i=0; $i < $this->Size(); $i++)
{
//get_class only returns the lowercase name
if(get_class($arrTemp[$i]) == strtolower($str))
{
$intPostition = $i;
break;
}
}
return $intPostition;
}
//END MANIPULATORS
}
?>
Please let me know your thoughts good and bad. I am fairly new to php so I just want input.PHP Code:<?php
class Vector
{
/***
* The array of objects
* @type array
***/
var $arrObjects = array();
/***
* The array capacity
* @type int
***/
var $intCapacity;
//CONSTRUCTOR
/***
* <p>Construct a new Vector</p>
* @param $intCapacity as int for the size of the Vector
***/
function Vector($intCapacity = 10)
{
$this->_setCapacity($intCapacity);
}
//END CONSTRUCTOR
//MANIPULATORS
/***
* <p>Adds an object to the Vector if the size of the vector is less than the
* capacity <code>[size() < capacity()]</code></p>
* @param $o the object to add to the vector
* @returns int 1 on success and int 0 on failure
***/
function add(&$o)
{
$blnAdded = 0;
if ($this->size() < $this->Capacity() && is_object($o))
{
$this->arrObjects[] = $o;
$blnAdded = 1;
}
return $blnAdded;
}
/***
* <p>Inserts the specified element at the specified position in this Vector.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).</p>
* @param $index int the position to add the object
* @param $o obj the object to add to the vector
* @returns int 1 on success and int 0 on failure
***/
function addAt($index, &$o)
{
$blnAdded = 0;
if(
$index >= 0 &&
$index <= $this->size() &&
$this->size() < $this->capacity() &&
is_object($o)
)
{
if($index == $this->size())
{
$this->arrObjects[$index] = $o;
$blnAdded = 1;
}
else
{
for($i = $this->size(); $i > $index; $i--)
{
$this->arrObjects[$i] = $this->arrObjects[$i-1];
}
$this->arrObjects[$index] = $o;
$blnAdded = 1;
}
}
return $blnAdded;
}
/***
* <p>Replaces the element at the specified position ($index) in this Vector with the
* specified element ($o).</p>
* @param $index int the position to set the object
* @param $o the object to add to the vector
* @returns int 1 on success and int 0 on failure
***/
function set($index, &$o)
{
$obj=0;
if($index >= 0 && $index <= $this->size())
{
$obj = $this->arrObjects[$index];
$this->arrObjects[$index] = $o;
}
return $obj;
}
/***
* <p>Sets the size of this Vector. If the new size is greater than the current size, new null items are
* added to the end of the Vector. If the new size is less than the current size, all components at
* index newSize and greater are discarded. </p>
* @param $intNewSize int set the size of the Vector
* @returns int 1 on success and int 0 on failure
***/
function setSize($intNewSize)
{
$blnSet = 0;
if($intNewSize >= 0 && $intNewSize < $this->capacity())
{
if($intNewSize >= $this->size())
{
for($i = $this->Size(); $i <= $intNewSize; $i++)
{
$this->arrObjects[$i] = null;
}
}
else
{
for($i = $intNewSize; $i <= $this->size(); $i++)
{
unset($this->arrObjects[$i]);
}
}
}
return $blnSet;
}
/***
* <p>Trims the capacity of this Vector to be the Vector's current size. If the capacity of this vector
* is larger than its current size, then the capacity is changed to equal the size. An application can
* use this to prevent the Vector from growing.</p>
* @returns void
***/
function trimToSize()
{
$this->_setCapacity($this->size());
}
/***
* <p>Removes the element at the specified position in this Vector. shifts any subsequent elements
* to the left (subtracts one from their indices). Returns the element that was removed from the Vector.</p>
* @param $index int removes the element at the $index in the Vector
* @returns obj that was removed on success and int 0 on failure
***/
function removeAt($index)
{
$o = 0;
if(!$this->isEmpty() && ($this->size() >= $index))
{
$o = $this->arrObjects[$index];
unset($this->arrObjects[$index]);
$this->arrObjects = array_values($this->_getObjects());
}
return $o;
}
/***
* <p>Removes all of the elements from this Vector. The Vector will be empty after this call.</p>
* @returns void
***/
function clear()
{
$this->arrObjects = array();
}
/***
* <p>Removes all components from this vector and sets its size to zero.</p>
* @returns void
***/
function removeAllElements()
{
$this->clear();
}
/***
* <p>Returns a copy of this vector.</p>
* @returns Vector a copy of this Vector
***/
function clone()
{
return new $this;
}
/***
* <p>Returns the current capacity of this Vector.</p>
* @returns void
***/
function capacity()
{
return $this->intCapacity;
}
/***
* <p>Returns the number of components in this Vector.</p>
* @returns int
***/
function size()
{
return sizeof($this->arrObjects);
}
/***
* <p>Returns whether the Vector is empty or not.</p>
* @returns int 1 if the Vector is empty and 0 otherwise
***/
function isEmpty()
{
(sizeof($this->arrObjects) > 0) ? $blnEmpty = 0
: $blnEmpty = 1;
return $blnEmpty;
}
/***
* <p>Returns the first object (the item at index 0) of this Vector.</p>
* @returns obj on success and 0 on failure
***/
function firstElement()
{
$o = 0;
if(!$this->isEmpty())
{
$o = $this->arrObjects[0];
}
return $o;
}
/***
* <p>Returns the last object (the item at index [size() - 1]) of this Vector.</p>
* @returns obj on success and 0 on failure
***/
function lastElement()
{
$o = 0;
if(!$this->isEmpty())
{
$lastElement = $this->size() - 1;
$o = $this->arrObjects[$lastElement];
}
return $o;
}
/***
* <p>Returns a string representation of this Vector,
* containing the String representation of each element. </p>
* @returns str
***/
function toString()
{
$str="";
for($i=0; $i < $this->size(); $i++)
{
$str .= "Object Name: " . get_class($this->arrObjects[$i]) . "<br>" .
"Object Position: " . $i . ",<br>";
}
return $str;
}
//END MANIPULATORS
//PRIVATE ACCESSORS
function _getObjects()
{
return $this->arrObjects;
}
function _setCapacity($value)
{
$this->intCapacity = $value;
}
//END ACCESSORS
}
//end Vector---------------------------------
?>
Kind regards,
php![]()









Bookmarks