EllisLab/CodeIgniter — Issue #598: _get_validation_object() returns always FALSE 
Author: tomas-vccp
Created: 2011-10-19 15:10:35+00:00
State: open

Version: CodeIgniter 2.0.3
System:	PHP Version 5.3.2-1ubuntu4.9
File: ./system/helpers/form_helper.php
Affected Code:

```php

function &_get_validation_object()
{
$CI =& get_instance();

// We set this as a variable since we're returning by reference
$return = FALSE;

if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
{
return $return;
}

$object = $CI->load->_ci_classes['form_validation'];

if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
return $return;
}

return $CI->$object;
}
```

the _get_validation_object() function always returns FALSE as the first if condition fails (because _ci_classes is a protected field of the Loader class). Also,

```php
$object = $CI->load->_ci_classes['form_validation'];
```

throws an error as, again, _ci_classes is a protected field.

Here is a suggested fix

```php
function &_get_validation_object()
{
$CI =& get_instance();

// We set this as a variable since we're returning by reference
$return = FALSE;

if ( !$CI->load->is_loaded('form_validation'))
{
return $return;
}

// this may need to be changed to a more suitable name
$object = 'form_validation';

if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
return $return;
}

return $CI->$object;
}
```

The fix uses the is_loaded method the Loader class already provides - The Law of Demeter prohibits access such as $CI->load->_ci_classes and although CI uses it extensively here is an example why it shouldn't!