object array's

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
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

object array's

Post 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++)&#123;
             
       $menuObj = $this -> menuArray&#1111;$i]; 
       
       if (($menuObj -> getMenuID()) == $menuId)&#123;
         // remove the menu from the array
         unset($this -> menuArray&#1111;$i]);  
         
         return true;
       &#125;
     &#125;
     // menu object wasn't found in the aray
     return false;    
   &#125;
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
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Extended functions are still accessed through $this->
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

Post 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.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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?
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

Post 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 &#123;

  var $menuID    = '';
  var $menuName  = '';
  var $menuClass = '';
  
  function  Menu($id, $name, $class)&#123;&#125;
  
  function setMenuID($id)&#123;&#125;
  
  function setMenuName($name)&#123;&#125;
  
  function setMenuClass($class)&#123;&#125;  
  
  function getMenuID()&#123;&#125;
  
  function getMenuName()&#123;&#125;  
 
  function getMenuClass()&#123;&#125;
  
  function toString()&#123;&#125;

  .
  .
  .
  .

&#125;
//------------------------------------

Code: Select all

class SubMenu extends Menu &#123;
  
  var $href = '';
  var $target = '';
  var $parentID = 0;
  
  function SubMenu($id, $pid, $name, $href, $class = '', $target = '_self')&#123;&#125;
  
  function setHref($href)&#123;&#125;
  
  function setTarget($target)&#123;&#125;
  
  function setParentID($pid)&#123;&#125;  
  
  function getParentID()&#123;&#125;
  
  function getHref()&#123;&#125;  
  
  function getTarget()&#123;&#125;
  
  function toString()&#123;&#125;

  .
  .
  .
  .
  
&#125;
// -----------------------------------------

Code: Select all

class MenuWorker &#123;
  
  /* 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())&#123; &#125;
  

  /* 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)&#123;&#125;
  
   
   /*
   * Return the number of menus in the global menu array.
   */
   function getNumMenus()&#123; &#125;
   
   
   /*
   * Returns whether or not the menu needs to be redrawn.
   */
   function isRedrawMenu()&#123; &#125;  

   /*
    * Removes a menu from the global menu array
    */
    function removeMenu($menuID)&#123;
 
    // code here from previous paste ............

   &#125;

  .
  .
  .
  .
     
&#125;
thanx again

g00fy
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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);
?>
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

Post 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
Post Reply