Page 1 of 1
object array's
Posted: Sat Jun 12, 2004 5:56 am
by mm00re
hi to all,
i have an array of objects. Menu Objects tp be precise. I have a Menu class and a subclass SubMenu that extends menu. (my background is java & c)
however when i iterate an array of both menu and submenu objects i can remove the menu objects ok but i cant remove the submenu objects using an inherited method from the menu class.
is this a php thing or is my syntax wrong ?
Code: Select all
MenuWorker.Class.php
function removeMenu($menuId){
if($this -> numMenus == 0){
return false;
}
for($i = 0; $i < ($this -> numMenus); $i++){
$menuObj = $this -> menuArrayї$i];
if (($menuObj -> getMenuID()) == $menuId){
// remove the menu from the array
unset($this -> menuArrayї$i]);
return true;
}
}
// menu object wasn't found in the aray
return false;
}
as i said the getMenuID() is inherited from Menu and i get this error when removing a submenu object.
Call to a member function on a non-object in C:\Program Files\Apache Group\Apache2\htdocs\tape_database\classes\MenuWorker.class.php on line 84
kind regards
g00fy
Posted: Sat Jun 12, 2004 7:57 am
by launchcode
Extended functions are still accessed through $this->
Posted: Sat Jun 12, 2004 9:57 pm
by mm00re
hey launchcode,
i realise that, but i dont have a need for that here.
i am iterating the array of menu objects and submenu objects(which extend the menu object)
i grab a reference to that object at each iteration
then try to call the menu objects getMenuID() method to check against input arg to MenuWorker.removeMenu() method.
it works for menu objects but not for the submenu objects that are extending the menu class.
all this is done from another class that has access to the menu and submenu classes.
what appears to be hppening is the getMenuID() method is not inherited for the submenu class for some reason.
so my main question is why the methods are not inherited from the superclass, or is my syntax wrong ?
kind regards,
g00fy.
Posted: Sun Jun 13, 2004 3:40 am
by launchcode
You said that MenuWorker extended Menu. In the small piece of code you posted, you had: $menuObj -> getMenuID(). You also said that this function (getMenuID) is part of Menu (i.e. inherited). Therefore you need to access it through $this->getMenuID() and not directly as you did in your code. See what I mean now?
Posted: Sun Jun 13, 2004 4:00 am
by mm00re
hey launchcode,
thanx again for the reply,
there must be some mixup here. Sorry i dont think i have been as clear as i could have. Here is the outline for my classes (the ones im having troubles with anyways
I have:
Code: Select all
class Menu {
var $menuID = '';
var $menuName = '';
var $menuClass = '';
function Menu($id, $name, $class){}
function setMenuID($id){}
function setMenuName($name){}
function setMenuClass($class){}
function getMenuID(){}
function getMenuName(){}
function getMenuClass(){}
function toString(){}
.
.
.
.
}
//------------------------------------
Code: Select all
class SubMenu extends Menu {
var $href = '';
var $target = '';
var $parentID = 0;
function SubMenu($id, $pid, $name, $href, $class = '', $target = '_self'){}
function setHref($href){}
function setTarget($target){}
function setParentID($pid){}
function getParentID(){}
function getHref(){}
function getTarget(){}
function toString(){}
.
.
.
.
}
// -----------------------------------------
Code: Select all
class MenuWorker {
/* array to hold the menu's and submenu's */
var $menuArray = null;
/* boolean value to check for redrawing */
var $redrawMenu = false;
/* count of menus in the array */
var $numMenus = 0;
/*
*
*/
function MenuWorker($array = array()){ }
/* Checks if input is a Menu Object, iff it is then it
* is added to the global menu array.
*
* Returns: True iff the input arg is a menu, after adding it
* to the menu array.
*/
function addMenu($menu){}
/*
* Return the number of menus in the global menu array.
*/
function getNumMenus(){ }
/*
* Returns whether or not the menu needs to be redrawn.
*/
function isRedrawMenu(){ }
/*
* Removes a menu from the global menu array
*/
function removeMenu($menuID){
// code here from previous paste ............
}
.
.
.
.
}
thanx again
g00fy
Posted: Sun Jun 13, 2004 4:30 am
by launchcode
Ok yes, I see what you mean. However I still cannot duplicate your error - see the following code which I just wrote to test out essentially what it is you're doing:
Code: Select all
<?php
class TestA
{
function HelloA ()
{
echo "Hi from TestA";
}
}
class TestB extends TestA
{
var $test;
function TestB ($value)
{
$this->test = $value;
}
function Welcome ()
{
echo "Hi from TestB #" . $this->test;
}
}
class Builder
{
var $obj_array = array();
function Builder ($obj_array)
{
$this->obj_array = $obj_array;
}
function TestItem ($value)
{
$test_obj = $this->obj_array[$value];
$test_obj->Welcome();
echo "<br>";
$test_obj->HelloA();
}
}
$a = new TestB(1);
$b = new TestB(2);
$c = new TestB(3);
$d = new TestB(4);
$obj_array[] = $a;
$obj_array[] = $b;
$obj_array[] = $c;
$obj_array[] = $d;
$test = new Builder($obj_array);
$test->TestItem(3);
?>
Posted: Sun Jun 13, 2004 5:32 am
by mm00re
thanx again lauchcode,
i found one little prob, being i wasnt decrementing the num_elements in array on removal. fixing that solved half of the problem but still getting the error
will have to look some more, your test code works fine
cant see y mine is giving me problems,
probably something simple, will come back to it later and probably find it straight away
thanx
g00y