#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171

    Explain this class


    Hello;

    There are 2 things in this class I like to know:

    1 - What is going on here: $this->CI =& get_instance();? What does it mean?

    2 - $this->CI->load->helper('form'); What is that? Loading a whole helper into a variable? How can a helper be loaded into a variable? I assume I need a little explaination on method chaining here too!

    PHP Code:
    class CI_Form_validation {

        protected 
    $CI;
        protected 
    $_field_data            = array();
        protected 
    $_config_rules        = array();
        protected 
    $_error_array            = array();
        protected 
    $_error_messages        = array();
        protected 
    $_error_prefix        '<p>';
        protected 
    $_error_suffix        '</p>';
        protected 
    $error_string            '';
        protected 
    $_safe_form_data        FALSE;

        
    /**
         * Constructor
         */
        
    public function __construct($rules = array())
        {
            
    $this->CI =& get_instance();

            
    // Validation rules can be stored in a config file.
            
    $this->_config_rules $rules;

            
    // Automatically load the form helper
            
    $this->CI->load->helper('form');

            
    // Set the character encoding in MB.
            
    if (function_exists('mb_internal_encoding'))
            {
                
    mb_internal_encoding($this->CI->config->item('charset'));
            }

            
    log_message('debug'"Form Validation Class Initialized");
        }

    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    question 1: first result from google search, http://stackoverflow.com/questions/4...i-get-instance

    question 2: first result from google search: http://ellislab.com/codeigniter/user...rm_helper.html


    about question 2
    I don't know anything about codeIgnighter but what I can tell, that bit of code is probably loading a file called form.php from a directory called helper
    this file according to the search result contains all of the functions for a form
    This seems to be just another way of doing an include or a require but through some sort of auto loader class
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    some comments in addition to the links Jack already gave you:

    Originally Posted by English Breakfast Tea
    1 - What is going on here: $this->CI =& get_instance();?
    The keyword you're looking for is reference.

    That code snippet is a leftover from old versions of CodeIgniter. They once needed this workaround for PHP 4, but since CodeIgniter requires PHP 5 now, it's become useless. They should have rewritten it. But I guess it's too much effort.



    Originally Posted by English Breakfast Tea
    2 - $this->CI->load->helper('form'); What is that? Loading a whole helper into a variable? How can a helper be loaded into a variable? I assume I need a little explaination on method chaining here too!
    Why do you think it's loading something into a variable? I don't see anything like that.

    The code calls the helper() method of the object stored in $this->CI->load.

    If you wanna know what exactly this object is and what it does, simply check the source code: system/core/Loader.php
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo