Jump to content

problems Referenceing and object


FutureAndAHope

Recommended Posts

Hi,

I have been creating an online application that stores information on a prospective bank loan, and prints it to screen at the end of the process. An Object is created called LoanApplication it contains an associative array ($this->Applicants) of Applicant objects, which contain information on each applicant. The LoanApplication object works fine but when an applicant is added to the LoanApplication using the method AddApplicant the new applicant object can not be referenced. I use PHPEd a debugger that can look at all objects and variables stored on the system. When the array is first created PHP stores an 'A' I asume meaning array, then once the object is added to the associative array it adds an 'O', normally PHPEd would show an actual array or and actual object (not just the string A, or O), but for some reason it is just storing the string 'O', or a string 'A'. Any ideas as to why the code below is not working specifically the line  $this->Applicants[$ApplicantId] = $applicant; The applicant ($applicant) object is created fine and contains all its values, the only thing I can think of is can PHP reference an object created in this way? I tried it like $this->Applicants[$ApplicantId] = new Applicant(); but this did not work either.


[b]CODE ADDING THE OBJECT[/b]

if($new_applicant)
        {
          $applicant = new Applicant();       
        }
       
        $applicant->Title = $Title;
        $applicant->FirstName = $FirstName;
        $applicant->LastName = $LastName;
        $applicant->DateOfBirth = $DateOfBirth;
        $applicant->MaritalStatus = $MaritalStatus;
        $applicant->Dependants = $Dependants;
        $applicant->DependantsAges = $DependantsAges;
        $applicant->DriversLicence = $DriversLicence;
        $applicant->Home = $Home;
        $applicant->Work = $Work;
        $applicant->Mobile = $Mobile;
        $applicant->Email =  $Email; 
        if($new_applicant)
        {
            $this->Applicants[$ApplicantId] = $applicant;
            $this->Applicants[$ApplicantId]->ApplicantId = $ApplicantId; 
        }
        else
        {   
            $ApplicantId = $applicant->ApplicantId;
            $this->Applicants[$ApplicantId] = $applicant;         
        }
       


[b]CLASSES REFERENCED ABOVE[/b]

class LoanApplication
{
    var $UniqueIdCounter = 0; 
    var $AmountToBorrow;
    var $LoanTerm;
    var $LoanTermOther;
    var $LoanPurpose;
    var $WhichLoan;
    var $PurposeOther;
    var $IsFor;
    var $Applicants;
    var $SameAddress;
   
    function LoanApplication()
    {
      $this->Applicants  = array(); 
    }
   
    function AddApplicant($Title, $FirstName, $LastName, $DateOfBirth, $MaritalStatus, $Dependants, $DependantsAges, $DriversLicence, $Home, $Work, $Mobile, $Email, $ApplicantId)
    {
        $new_applicant = true;
        $applicant = null;
        foreach($this->Applicants as $Appl)
        {
            if($Appl->ApplicantId == $ApplicantId)
            {
                $applicant = $Appl;
                $new_applicant = false; 
            }       
        }
        if($new_applicant)
        {
          $applicant = new Applicant();       
        }
       
        $applicant->Title = $Title;
        $applicant->FirstName = $FirstName;
        $applicant->LastName = $LastName;
        $applicant->DateOfBirth = $DateOfBirth;
        $applicant->MaritalStatus = $MaritalStatus;
        $applicant->Dependants = $Dependants;
        $applicant->DependantsAges = $DependantsAges;
        $applicant->DriversLicence = $DriversLicence;
        $applicant->Home = $Home;
        $applicant->Work = $Work;
        $applicant->Mobile = $Mobile;
        $applicant->Email =  $Email; 
        if($new_applicant)
        {
            $this->Applicants[$ApplicantId] = $applicant;
            $this->Applicants[$ApplicantId]->ApplicantId = $ApplicantId; 
        }
        else
        {   
            $ApplicantId = $applicant->ApplicantId;
            $this->Applicants[$ApplicantId] = $applicant;         
        }
       
    }
   
    function AddProperty($Value, $Suburb, $Description, $Property)
    {
      $Propertyob = new Property();
      $Propertyob->Value = $Value;
      $Propertyob->Suburb = $Suburb;
      $Propertyob->Description = $Description;
      $Propertyob->Property = $Property;
      $this->Properties[] = $Propertyob;
    } 
   
    function AddAsset($Description, $Property)
    {
        $this->Description = $Description;
        $Property->Property = $Property;     
    }

}


class Applicant
{
  var $ApplicantId;
  var $Title;
  var $FirstName;
  var $LastName;
  var $DateOfBirth;
  var $MaritalStatus;
  var $Dependants;
  var $DependantsAges;
  var $DriversLicence;
  var $Home;
  var $Work;
  var $Mobile;
  var $Email;
 
  var $Loans = array();
  var $Cards = array();
  var $Properties = array();
  var $Asset = array();
  var $Vehicles = array();
  var $PersonalEffects = array();
  var $Employers = array();
  var $OtherIncome = array();
  var $Residence;
 
  function FullName(){
      return $FirstName . " " . $LastName;
  }
 
  function AddOtherIncome($Describe, $Amount, $Frequency)
  {
    $OtherIncome = new OtherIncome(); 
    $OtherIncome->Describe = $Describe;
    $OtherIncome->Amount = $Amount;
    $OtherIncome->Frequency = $Frequency;
    $this->OtherIncome[] = $OtherIncome;         
  }
 
  function AddResidence($postcode, $suburb, $streetaddress, $residenceMonths, $residenceYears)
  {
      $residence = new Residence();
      $residence->Postcode = $postcode;
      $residence->Suburb = $suburb;
      $residence->StreetAddress = $streetaddress;
      $residence->ResidenceMonths = $residenceMonths;
      $residence->ResidenceYears = $residenceYears;
      $this->Residence = $residence;
  }
 
  function AddPersonalEffect($name, $value)
  {
        $PersonalEffects = new PersonalEffect();
        $PersonalEffect->Name = $name;
        $PersonalEffect->Value = $value;
        $this->PersonalEffects[] = $PersonalEffect;   
  }
   
  function AddVehicle($name, $value)
  {
        $MotorVehicles = new MotorVehicles();
        $MotorVehicles->Name = $name;
        $MotorVehicles->Value = $value;
        $this->MotorVehicles[] = $MotorVehicles;
         
  }         
   
    function AddLoan($LeftOnLoan, $MinimumRepayments, $Frequency, $Left)
    {
        $Property = new Loan();                 
        $Property->LeftOnLoan = $LeftOnLoan;
        $property->MinimumRepayments = $minimumRepayments;
        $property->Frequency = $Frequency;
        $property->Left = $Left;             
        $this->Loans[]  = $Property;
    }
   
    function AddCard($Limit, $Balance)
    {
        $card = new Cards();
        $card->Limit = $Limit;
        $card->Balance = $Balance;
        $this->cards[] = $card; 
    } 
 
    function AddEmployerDetails($OccupationDescription, $EmploymentType, $EmployersName, $Income, $EmploymentDurationMonths, $EmploymentDurationYears)
    {
      $employer = new EmployerDetails();                   
      $employer->OccupationDescription =          $OccupationDescription;
      $employer->EmploymentType =                  $EmploymentType;
      $employer->EmployersName =                    $EmployersName;
      $employer->Income =                              $Income;
      $employer->EmploymentDurationMonths =    $EmploymentDurationMonths;
      $employer->EmploymentDurationYears =      $EmploymentDurationYears;
      $this->Employers[] = $employer;   
    }     
}

[b]ADDITION CODE FOR YOUR LIFE[/b]

I know the thoughts I think toward you says the LORD for good and not evil, to give you a future and a hope. You will call to me, and I will answer you when you search for me with your whole heart.
Link to comment
https://forums.phpfreaks.com/topic/31059-problems-referenceing-and-object/
Share on other sites

I don't think this is a coincidence:

[code=php:0]<?php
$a = array();
$b = new stdClass;
$a_string = (string)$a;
$b_string = (string)$b;
print "Array as string: $a_string\n";
print "Object as string: $b_string\n";
print "String 'Array' indexed by string: {$a_string[$a_string]}\n";
print "String 'Object' indexed by string: {$b_string[$b_string]}\n";
?>
[/code]


See how the A and O come into existence?  I'm pretty sure there's something like that in your code, though I haven't been able to find it.
Hi,

I found the offending line of code, Applicants had originally been used a just a numbered counter, numbering how many applicants there were. So it was being set to a number i.e. "1", "7". The problem was then when the object was being assigned it was assigning it to the string like you mentioned.

e.g. $LoanApplication->Applicants= "1";
      $ApplicantId = 0;
      //Because  $LoanApplication->Applicants is a string  $applicant was being cast to a string
      $LoanApplication->Applicants[$ApplicantId] = $applicant // now the string "Object" so stored as "O"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.