Constructors getting called ?

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
mikeym
Forum Newbie
Posts: 20
Joined: Sun Dec 16, 2007 8:07 am

Constructors getting called ?

Post by mikeym »

Hi

can anyone tell me why my constructors and destructors are not being called?

Here's the output after a form is submitted so FormManager.php is called directly (note where the echos are in the constructor):
Create
And here's the - rather long - code:

Code: Select all

 
<?php
/**
 * Form Managing class
 *
 * Version: 0.3.2b
 *
 * @author mikey <>
 *
 */
 
/**
 * Form Manager class
 */
class FormManager {
 
    var $_hLayout; // handle to callback function for managing Layout of form
    var $_LayoutParams = array(); // Layout parameters to pass to layout function
    var $_hProcess;  // handle to callback function for managing the Processing of the form
    var $_boxes; // array holding the details of each box
    var $_page; // the page that the form was created on
    var $_validator; // the javascript function to use for validating the form
    var $_name; // the name of the the form
    var $_id; // the id of the form
    var $_key; // a unique key to identify the form
    // static $_names = array(); // array of names to identify each form on the page and ensure it is unique.
    var $_values = array();  // holds values of the fields from a submitted form
 
    var $_errors = array();  // holds error messages produced when a form is processed
 
    var $_num_errors;   // the number of errors in a form
    var $_errors_name; // the name of the form that produced the errors
    
    /**
     * Class constructor
     *
     * @access private
     */
 
    function __construct() {
        // start a session (should be called by session.php)
        session_start();
        /**
 
         * Get the unique key for the data for this page
 
         */
        echo "Construct<br />";     
        if(!isset($_POST['_key'])) {
            // Create a random unique key if one not set
            $this->_key = "key".(rand()%999999);
            $i=0;
            while(isset($_SESSION[$this->_key])) {
                $i = $i + 1;
                if ( $i > 99 ) {
                    echo "Woopse something went very wrong! \n";
                }
                $this->_key = "key".(rand()%999999);
            }
            // Form being created on a new page so get the URL
            $this->_page = $_SESSION['url'];
            $this->_errors_name = $_SESSION['errors_name'];
            echo "New Form<br />";
        } else {
            // Get the key passed as part of the form
            $this->_key=$_POST['_key'];
            // Read the parameter array indexed by the key
            $parameters=$_SESSION[$this->_key];
 
            /**
 
             * Make sure that internal variables persist after the call for processing
 
             */
 
            if(isset($parameters['boxes'])) {
 
                $this->_boxes = $parameters['boxes'];
 
            } else {
                $this->_boxes = array ();
            }
            if(isset($parameters['hLayout'])) {
 
                $this->_hLayout = $parameters['hLayout'];
            } else {
                $this->_hLayout = 0;
            }
            if(isset($parameters['hProcess'])) {
 
                $this->_hProcess = $parameters['hProcess'];
            } else {
 
                $this->_hProcess = 0;
 
            }
            if(isset($parameters['page'])) {
 
                $this->_page = $parameters['page'];
            } else {
 
                $this->_page = '';
 
            }
            if(isset($parameters['validator'])) {
 
                $this->_validator = $parameters['validator'];
            } else {
 
                $this->_validator = '';
 
            }
            if(isset($parameters['name'])) {
 
                $this->_name = $parameters['name'];
            } else {
 
                $this->_name = '';
 
            }
            if(isset($parameters['id'])) {
 
                $this->_id = $parameters['id'];
            } else {
 
                $this->_id = '';
 
            }
            $this->_values = $_POST;
            echo $this->_key . "<br />\n";
            echo $this->_hLayout . "<br />\n";
            echo $this->_hProcess . "<br />\n";
            echo $this->_page . "<br />\n";
            echo $this->_validator . "<br />\n";
            echo $this->_name . "<br />\n";
            echo $this->_id . "<br />\n";           
            $this->_process();
 
        }
    }
 
    /**
     * Class destructor
     *
     * @access private
     */
 
    function __destruct() {
        $_SESSION['value_array'] = $this->_values;
        $_SESSION['error_array'] = $this->_errors;
        $_SESSION['errors_name'] = $this->_name;
        $_SESSION[$this->_key]=array("boxes" => $this->_boxes, "hLayout" => $this->_hLayout, "hProcess" => $this->_hProcess, "page" => $this->_page, "validator" => $this->_validator, "name" => $this->_name, "id" => $this->_id);
    }
 
    /**
     * Add a box
     *
     * @access public
     * @param string $name The name of the box
     * @param string $type i.e. "text", "textarea", "submit", "hidden", "file", "button"
     * @param array() $parameters parameters
     * @param string $value string value of input
     * @return bool
     */
    function addBox ($name, $type, $parameters, $value) {
        if (isset ($this->_boxes[$name])) {
            echo "Woopse something went very wrong! - Cannot add form box \"$name\" because it already exists! \n";
            return false; // already exists
        }
        if (!preg_match ('/^[a-zA-Z0-9*_!+-]+$/', $name)) {
            echo "Woopse something went very wrong! - Cannot add form box \"$name\" because it is an invalid name! \n";
            return false; // invalid name
        }
        if ($type != "textarea" && $type != "submit" && $type != "hidden" && $type != "text" && $type != "file" && $type != "button") {
            echo "Woopse something went very wrong! - Cannot add form box \"$name\" because type \"$type\" is invalid! \n";
            return false; // invalid type
        }
        $this->_boxes[$name] = array (
            'name' => $name,
            'type' => $type,
            'parameters' => $parameters,
            'value' => $value,
        );
        return true;
    }
 
    /**
     * Retrieve a box
     *
     * @access public
     * @param $name The box to get
     * @return array (box)
     */
    function getBox ($name) {
        if (isset ($this->_boxes[$name])) {
            return $this->_boxes[$name];
        }
        //echo "Woopse something went very wrong! - Cannot get form box \"$name\" because it doesn't exist! \n";
        return false;
    }
 
    /**
     * Set a box
     *
     * @access public
     * @param $name The box to set
     * @param string $type i.e. "textarea", "submit", "hidden"
     * @param array() $parameters parameters
     * @param string $value string value of input
     * @return bool
     */
    function setBox ($name, $type, $parameters, $value) {
        $res = $this->removeBox($name);
        $res2 = $this->addBox($name, $type, $parameters, $value);
        if ($res && $res2) {
            return true;
        } else {
            echo "Woopse something went very wrong! - Cannot set form box \"$name\" an problem was encountered! ";
            return false;
        }
    }
 
    /**
     * Set a callback function for processing form
     *
     * @access public
     * @param $hProcess handle to the callback function
     * @return bool
     */
    function setCallbackProcess ($hProcess) {
        if (is_callable($hProcess)) {
            $this->_hProcess = $hProcess;
            return true;
        } else {
            echo "Woopse something went very wrong! - Cannot set form processing function \"$hProcess\" not a valid function! \n";
            return false;
        }
    }
 
    /**
     * Set a callback function for laying out form
     *
     * @access public
     * @param $hLayout handle to the callback function
     * @return bool
     */
    function setCallbackLayout ($hLayout) {
        if (is_callable($hLayout)) {
            $this->_hLayout = $hLayout;
            return true;
        } else {
            echo "Woopse something went very wrong! - Cannot set form layout function \"$hLayout\" not a valid function! \n";
            return false;
        }
    }
 
    /**
     * Add a layout parameter to pass to layout function
     *
     * @access public
     * @param $name string, name of parameter to pass to the callback function
     * @param $value string, value of parameter to pass to the callback function
     * @return bool
     */
    function addLayoutParam ($name, $value) {
        if (!isset($this->_LayoutParams[$name])) {
            $this->_LayoutParams[$name] = $value;
            return true;
        } else {
            echo "Woopse something went very wrong! - Cannot add layout parameter \"$name\" as it is already set! \n";
            return false;
        }   
    }
 
    /**
     * Set name of validating javascript function for form
     *
     * @access public
     * @param string $validator name of validating javascript function 
     */
    function setValidator ($validator) {
        $this->_validator = $validator;
    }
 
    /**
     * Load errors if this form is the form that has been submitted
     *
     * @access private
     * @param string $name 
     */
    function _LoadErrors ($name) {
        if($name==$this->_errors_name) {
                /*
 
             * Get form value and error arrays, used when there
 
             * is an error with a user-submitted form.
 
             */
 
            if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
 
                $this->_values = $_SESSION['value_array'];
 
                $this->_errors = $_SESSION['error_array'];
 
                $this->_num_errors = count($this->_errors);
 
 
 
                unset($_SESSION['value_array']);
 
                unset($_SESSION['error_array']);
 
            } else {
 
                $this->num_errors = 0;
 
            }   
        } else {
            $this->num_errors = 0;
        }
    }
 
    /**
 
     * Records the value typed into the given field by the user.
     * The form must be given a unique name to use the field values.
 
     * Fields submitted by $_POST are automatically managed by the FormManager.
     * 
     * @param string $field the field to set
     * @param string $value the value to give the $field
     * @return bool 
 
     */
 
    function setFieldValue($field, $value) {
        if (!isset($this->_name)) {
            echo "Woopse something went very wrong! - Cannot set value for \"$field\" as the form has not been given a name! \n";
            return false;
        }
 
        $this->_values[$field] = $value;
        return true;
 
    }
 
 
 
    /**
 
     * Records new form error.
     * The form must be given a unique name to use the error.
     * 
     * @param string $field the field to set
     * @param string $errmsg the error message to show
     * @return bool 
 
     */
 
    function setError($field, $errmsg){
        if (!isset($this->_name)) {
            echo "Woopse something went very wrong! - Cannot set error message for \"$field\" as the form has not been given a name! \n";
            return false;
        }
 
        $this->_errors[$field] = $errmsg;
 
        $this->_num_errors = count($this->_errors);
        return true;
 
    }
 
 
 
    /**
 
     * Returns the value attached to the given field, if none exists, the empty string is returned.
     * 
     * @param string $field the field to return the value from
     * @return string
 
     */
 
    function getFieldValue($field){
 
        if(array_key_exists($field,$this->_values)){
 
            return htmlspecialchars(stripslashes($this->_values[$field]));
 
        }else{
 
            return "";
 
        }
 
    }
 
 
 
    /**
 
     * Returns the error message attached to the given field, if none exists, the empty string is returned.
     * 
     * @param string $field the field to return the value from
     * @return string
 
     */
 
    function getError($field){
 
        if(array_key_exists($field,$this->_errors)){
 
            return "<font size=\"2\" color=\"#ff0000\">".$this->_errors[$field]."</font>";
 
        }else{
 
            return "";
 
        }
 
    }
 
    /**
     * Set name of form. Return true is successful
     *
     * @access public
     * @param string $name 
     * @return bool
     */
    function setName ($name) {
        static $_names = array();
        if(isset($this->_names[$name])) {
            echo "Woopse something went very wrong! - The name of form cannot be set. \"$name\" is already in use! \n";
            return false;
        }
        $this->_names[$name] = 1;
        $this->_LoadErrors($name);
        $this->_name = $name;
        return true;
    }
 
    /**
     * Set id of form
     *
     * @access public
     * @param string $id 
     */
    function setid ($id) {
        $this->_id = $id;
    }
    
    /**
     * Remove a box
     *
     * @access public
     * @param $name The box to remove
     * @return bool
     */
    function removeBox ($name) {
        if (isset ($this->_boxes[$name])) {
            unset ($this->_boxes[$name]);           
            return true;
        }
        echo "Woopse something went very wrong! - Cannot remove form box \"$name\" because it doesn't exist! \n";
        return false;
    }
    
    /**
     * Remove all boxes
     *
     * @access public
     */
    function removeAllBoxes () {
        $this->_boxes = array ();
    }
    
    /**
     * Number of boxes
     *
     * @access public
     * @return int
     */
    function numboxes () {
        return count($this->_boxes);
    }
    
    /**
     * Process the form by calling processing callback function (should be called 
     * by form produced by form manager). Passes the formmanager and refering page as parameters
     *
     * @access private
     */
    function _process() {
        global $session;
        // Ensure that we are still logged on before processing the form
        if ($session->logged_in) {
            // Pass parameters to the processing function
            $parameters['formmanager']=$this;
            $parameters['referer']=$this->_page;
            $parameters['isAdmin']=$session->isAdmin();
            $parameters['user']=$session->username;
            if (is_callable($this->_hProcess)) {
                $res = call_user_func_array($this->_hProcess, $parameters);
            }
        } else {
            echo "Sorry you're no longer logged on! Please try logging in and trying again. ";
        }
    }
 
    /**
     * Access the first box on the form returns the box name if found and false if not 
     *
     * @access public
     * @return string $name or false
     */
    function firstBox() {
        $value=reset($this->_boxes);
        if ($value) {
            $name=key($this->_boxes);
            return $name;
        } else {
            return false;
        }
    }
 
    /**
     * Access the next box on the form returns the box name if found and false if not 
     *
     * @access public
     * @return string $name or false
     */
    function nextBox() {
        $value=next($this->_boxes);
        if ($value) {
            $name=key($this->_boxes);
            return $name;
        } else {
            return false;
        }
    }
 
    /**
     * Access the current box on the form returns the box name if found and false if not 
     *
     * @access public
     * @return string $name or false
     */
    function currentBox() {
        $value=current($this->_boxes);
        if ($value) {
            $name=key($this->_boxes);
            return $name;
        } else {
            return false;
        }
    }
 
    /**
     * The type of the current box on the form. Returns false if not found
     *
     * @access public
     * @return string $type or false
     */
    function BoxType() {
        $value=current($this->_boxes);
        if ($value) {
            return $value['type'];
        } else {
            return false;
        }
    }
 
    /**
     * The value of the current box on the form. Returns false if not found
     *
     * @access public
     * @return string $value or false
     */
    function BoxValue() {
        $value=current($this->_boxes);
        if ($value) {
            return $value['value'];
        } else {
            return false;
        }
    }
 
    /**
     * The Parameters of the current box on the form. Returns false if not found
     *
     * @access public
     * @return array $parameters or false
     */
    function BoxParameters() {
        $value=current($this->_boxes);
        if ($value) {   
            return $value['parameters'];
        } else {
            return false;
        }
    }
 
/*
<form action="include/process.php" name="post" method="post"  onsubmit=" return form_Validator2(this)">
<input class="form" tabindex="1" id="heading" name="heading" value="" />
<textarea class="formtext" style="width: 730px; height: 150px;" tabindex="2" id="bbcode" name="message" onclick="javascript&#058;storeCursor('bbcode');" onkeyup="javascript&#058;storeCursor('bbcode');" onchange="javascript&#058;storeCursor('bbcode');" onselect="javascript&#058;storeCursor('bbcode');"></textarea>
<input type="submit" name="post" class="submit" value="Submit News" />
<input type="hidden" name="id" value="" />
<input class="file"  type="file" name="browse" id="file_input" accept="image/jpeg" maxlength="100000" />
 
/*
 
    /**
     * Render a box to HTML
     *
     * @access private
     */
    function _RenderBox($name, $type, $parameters, $value) {
        switch ($type) {
            case 'text':
                $text = "<input name=\"$name\" value=\"$value\" ";
                $close = "/>\n";
                break;
            case 'textarea':
                $text = "<textarea name=\"$name\" ";
                $close = ">$value</textarea>\n";
                break;
            default:
                $text = "<input name=\"$name\" type=\"$type\" value=\"$value\" ";
                $close = "/>\n";
        }
        // add any parameters
        $val=reset($parameters);
        while($val) {
            $key=key($parameters);
            $text = $text . "$key=\"$val\" ";
            $val=next($parameters);
        }
        echo $text . $close;
    }
 
    /**
     * Render current box to HTML. Returns false if no box found
     *
     * @access public
     * @return string or false
     */
    function RenderCurrentBox() {
        $name = currentBox();
        if ($name) {    
            $type = $this->BoxType();
            $parameters = $this->BoxParameters();
            $value = $this->BoxValue();
            return $this->_RenderBox($name, $type, $parameters, $value);
        } else {
            return false;
        }
    }
 
    /**
     * Render box $name to HTML. Returns false if no box found
     *
     * @access public
     * @param $name The box to render
     * @return string or false
     */
    function RenderBox($name) {
        $box = $this->getBox($name);
        if ($box) { 
            $type = $box['type'];
            $parameters = $box['parameters'];
            $value = $box['value'];
            return $this->_RenderBox($name, $type, $parameters, $value);
        } else {
            return false;
        }
    }
 
    /**
     * Render Form to HTML
     *
     * @access public
     */
    function Render() {
        $text = "<form action=\"include/FormManager.php\" name=\"" . $this->_name . "\" id=\"" . $this->_id . "\" method=\"post\" ";
        $box = $this->firstBox();
        while($box) {
            if ($this->BoxType() == "file") {
                $text = $text . "enctype=\"multipart/form-data\" ";
                break;
            }
            $box = $this->nextBox();
        }
        if ($this->_validator == '') {
            $text = $text . "onsubmit=\"\" >\n";
        } else {
            $text = $text . "onsubmit=\" return " . $this->validator . "(this)\" >\n";
        }
        echo $text;
        //call the layout callback function to produce a HTML rendering of the form body
        $parameters = $this->_LayoutParams;
        $parameters['formmanager']=$this;
        $parameters['referer']=$this->_page;
        if (is_callable($this->_hLayout)) {
            $res = call_user_func_array($this->_hLayout, $parameters);
        }
        // Add the key to the form for the form manager
        echo "<input type=\"hidden\" name=\"_key\" value=\"" . $this->_key . "\" /></form>\n";
    }
 
    // Template for retrieving elements from an array
/*  function get_elements($parameters) {
        $value=reset($parameters);
        while($value) {
            $key=key($parameters);
            echo "\"$key\" => \"$value\"\n";
            $value=next($parameters);
        }
    }*/
    
}
 
include_once "session.php";
/*
 * Create an instance of FormManager if it is being called directly to process a form
 */
if ($_SESSION['url'] == "/include/FormManager.php") {
    echo "Create<br />";
    include_once "images.php";
    include_once "comments.php";
    include_once "documents.php";
    $formmanager = new FormManager;
}
?>
 
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Constructors getting called ?

Post by superdezign »

Are you using PHP 4 or PHP 5?

The 'var' keyword and the need for '@access' tags in the documentation were deprecated as of PHP 5. Also, PHP 4 does not support the magic functions such as __construct() and __destruct().
mikeym
Forum Newbie
Posts: 20
Joined: Sun Dec 16, 2007 8:07 am

Re: Constructors getting called ?

Post by mikeym »

Ahhh...
I' m version 4.4.2. Is there anything I can do to get this functionality?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Constructors getting called ?

Post by superdezign »

In PHP 4, constructors are he same name as the class.

Code: Select all

class FormManager {
  /**
   * Constructor
   */
  function FormManager() {
    // Constructor
  }
}
My personal suggestion would be to upgrade to PHP 5, though. :P
PHP 4 does not support custom destructors.
mikeym
Forum Newbie
Posts: 20
Joined: Sun Dec 16, 2007 8:07 am

Re: Constructors getting called ?

Post by mikeym »

Darn! It's the destructor that I really needed.

Ohh...well...

Unfortunately the site is hosted so there's no option to upgrade. Thanks anyway.
Post Reply