OOP PHP destruct()

Discussion in 'PHP' started by anna.sobiepanek, Dec 19, 2007.

  1. #1
    I dont understand the default destructor
    here is some of my class:
    class Submission{

    private $_clientid = "";
    private $_agentid = "";
    private $_agentcase = "";
    private $_payment = "";
    private $_shipment = "";
    private $_typeoftesting = "";
    private $_date = "0000-00-00";
    private $_taxpercent = 0;
    private $_tax = 0;
    private $_fuelcharge = 0;
    private $_notes = "";
    private $_userid = "";
    private $_status = "";
    private $_currency = "";
    private $_balance = 0.00;
    private $_individuals = Array();
    private $_numofind = 0;
    private $_subm_id = "";
    private $_repDate = "0000-00-00 00:00:00";
    private $_repLogo = "";
    private $_repHypo = "";
    private $_repConc = "";
    private $_repNote = "";

    //default
    function __construct()
    {
    $this->_clientid = "";
    $this->_agentid = "";
    $this->_agentcase = "";
    $this->_payment = "";
    $this->_shipment = "";
    $this->_typeoftesting = "";
    $this->_date = "0000-00-00";
    $this->_taxpercent = 0;
    $this->_tax = 0;
    $this->_notes = "";
    $this->_userid = "";
    $this->_status = "";
    $this->_currency = "";
    $this->_numofind = 0;
    $this->_balance = 0.00;
    $this->_individuals[0] = NULL;
    $this->_subm_id = "";
    $this->_repDate = "0000-00-00 00:00:00";
    $this->_repLogo = "";
    $this->_repHypo = "";
    $this->_repConc = "";
    $this->_repNote = "";
    }
    function __destruct()
    {
    print "destroying submittion";
    $payment = $this->getPayment();
    $shipment = $this->getShipment();
    unset($payment);
    unset($shipment);
    /*I had to add this because when i used isset($Submission) after i called this destructor it returned true and gave me a value for getSubmID(""); */
    $this->setSubmID("");
    unset($this);
    }

    the code that uses this class:
    <?php
    include("Scripts/functions.php");
    include("Scripts/user.php");
    include("Scripts/submission.php");
    include("Scripts/test.php");
    include("Scripts/profiles.php");
    include("Scripts/sample.php");
    include("Scripts/result.php");
    include("Scripts/individual.php");
    include("Scripts/calcpatindex.php");

    #################
    # Session Start #
    #################
    session_start();
    $User = $_SESSION['User'];
    $user = $User->getUsername();
    $pass = $User->getPassword();

    get_connection($user,$pass);

    ##########################
    # Get External Variables #
    ##########################
    $subm = $_REQUEST['id'];
    $sav = $_REQUEST['submit'];
    $logo = $_REQUEST['logo'];
    $tem = $_REQUEST['temp'];
    $savv = $_REQUEST['savv'];
    $Submission = $_SESSION['Submission'];

    ##########################
    $tm = 0;
    $van = "";

    if($_REQUEST['close'] !="")
    {
    #print_r($Submission);
    $Submission->deleteAllIndividual();
    unset($Submission);
    unset($_SESSION['Submission']);
    $subm == "";
    $_REQUEST['close'] =="";
    }
    if($_REQUEST['save'] !="")
    {
    $Submission->setRepConc($_POST['conclusion']);
    $Submission->setRepHypo($_POST['hypo']);
    $Submission->setRepNote($_POST['note']);
    $subm = $Submission->getSubmID();
    $_REQUEST['save'] =="";
    }
    if($_REQUEST['delete'] !="")
    {
    $Submission->setRepConc("");
    $Submission->setRepHypo("");
    $Submission->setRepNote("");
    $subm = $Submission->getSubmID();
    $_REQUEST['delete'] =="";
    }
    if ($subm != "")
    {
    #if the submission isn't already set then retrieve data and set it
    if($Submission == NULL)
    {
    $Submission = retrieveSubmission($subm);

    }
    $Submission->setLogo($logo);
    ........main code block.......
    }
    so 2 problems:#1 when i press close or save it goes into the appropriate $_REQUEST['save'] !="" ... code block and then goes back to the beginning of the code and sets $subm = $_REQUEST['id']; which is now nothing shouldn't the code just flow to the end and go into the if($subm <> "") block??
    #2 when i try to get around problem 1 and add to the beginning line $Submission = $_SESSION['Submission']; this code
    if(isset($Submission))
    {
    $subm = $Submission->getSubmID();
    }
    even when i press close and the destruct unsets($Submission) the $subm is not nothing which means that the destruct didn't really work so now my question what are u suppose to do in the default destruct() are u suppose to set all of ur variables to NULL what does unset($this) really do???
     
    anna.sobiepanek, Dec 19, 2007 IP
  2. anna.sobiepanek

    anna.sobiepanek Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    function goto(url)
    {
    myForm.action = myForm.action + url;
    myForm.submit();
    <!--myForm.action = "reports_b.php";-->
    }
    I used this javascript function to get the $_REQUEST['close'] ...... the my.Form.action= "reports_b.php" caused the page to reload for some reason. I thought only myForm.submit() does that... when i commented the last line in the function everything worked fine...
    I still want to know about the destructor though I don't want any memory leaks...
     
    anna.sobiepanek, Dec 19, 2007 IP
  3. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The destruct function will be called when the object is destroyed or the scripts execution is killed.

    So in your case if you try and destroy an instance of your Submission class, you should get the 'destroying submission' message and it will carry out the rest of the code.

    You dont need to call __destruct directly, it is called automatically.

    Hope this helps
     
    tonybogs, Dec 19, 2007 IP