Here is my code. Two files: 1) myclass.php 2)test.php to instantiate the
objects.

I have two classes. One is a "parts" class with some basic properties in an
array. And the other class is the "repair" class which needs to hold MANY
parts. In the "repair" class, I have a $partsarray so that I can just add
them to the array and then process them later.

The problem is that when I need to get the parts out of the repair class -
they are not there. I made a method called getPartsCount and it always
returns 0. Can someone take a look at this code and help me?  Thanks in
advance.

<?php
//THIS IS MYCLASS.PHP

//make a record class
class part {
  //properties
  var  $_data = array();

  //methods
  function getData($attributeName) {
    return $this->_data[$attributeName];
  }
  function setData($attributeName, $value) {
    $this->_data[$attributeName] = $value;
  }
}

//make a REPAIR class
class repair {
  //properties
  var $_data = array();
  var $_partsarray = array();

  //methods
  function getData($attributeName) {
    return $this->_data[$attributeName];
  }
  function setData($attributeName, $value) {
    $this->_data[$attributeName] = $value;
  }

     function getPart($pos) {
      return $this->_partsarray[$pos];
     }
  function addPart($partsclass, $pos) {
   //pos will actually be a string value
     $this->_partsarray['$pos'] = $partsclass;
  }
  function deletePart($pos) {
    unset($this->_partsarray['$pos']);
  }

  function countParts() {
    return count($_partsarray);
  }

  function getParts() {
    return $_partsarray;
  }
}
?>

<?php
// THIS IS THE CODE TO TEST THE CLASSES - TEST.PHP
require_once('myclass.php');

//make the parts
$partsclass = new part;
$partsclass->setData('1', 'val1');
$partsclass->setData('2', 'val2');
$partsclass->setData('3', 'val3');

$partsclass2 = new part;
$partsclass2->setData('1', 'val1');
$partsclass2->setData('2', 'val2');
$partsclass2->setData('3', 'val3');

$repairclass = new repair;
$repairclass->setData('test1', 'value1');
$repairclass->addPart($partsclass1, '1');
$repairclass->addPart($partsclass2, '2');

echo 'the test value is' . $repairclass->getData('test1');
echo '<br>and the part counter is: ' . $repairclass->countParts();

$partsclasstemp = new part;
$partsclasstemp = $repairclass->getPart('1');
echo '<b><P>parts data is: ' . $partsclasstemp->getData('2');
echo '<br>';
$temparray = array();
$temparray = $repairclass->getParts();

echo count($temparray);
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to