Welcome Guest, Not a member yet? Register   Sign In
Validation in autoload PHP 4 (Fix suggested)
#1

[eluser]Seppo[/eluser]
This is an old bug... It has been reported many times, and currently it´s in the tracker with the ID 3175

The original problem is that in the view you get an empty error_string even if the validation fails.
Tracking it down, I found out that the problem was that PHP was generating a copy of the autoloaded class, so they were different in the controller class and in the loader class (where the $this variable point, inside the views).
A quick example, to show this

Code:
// system/application/config/autoload.php
$autoload['libraries'] = array('validation');

// system/application/controllers/bug3175.php
class Bug3175 extends Controller {
    function Bug3175()
    {
        parent::Controller();
    }

    function index()
    {
        $this->validation->hello_world = 'abcde';
        $this->load->view('bug3175');
    }
}

// system/application/controllers/bug3175.php
echo $this->validation->hello_world;

In PHP 4 (I´ve tested this in 4.3.9 and 4.4.7) throws a notice for the undefined variable. In PHP 5 (5.0.0, 5.1.6, 5.2.5) it prints "abcde", as expected.



After tracking this bug inside all core I´ve finally reached to system/libraries/Controller.php, on line 87. After $this->_ci_autoloader(); I´ve added
Code:
foreach (array_keys(get_object_vars($this)) as $attribute)
            {
                if (is_object($this->$attribute))
                {
                    $this->load->$attribute =& $this->$attribute;
                }
            }
which syncronize back the references between the Loader and the Controller.

PLEASE EVERYONE, test this on all PHP versions you can and submit the result, so we can finally fix this bug...
#2

[eluser]Derek Jones[/eluser]
Thanks for looking into this Seppo. It appears this affects all libraries that call the CI object. I think the only thing I would change in your proposed fix is to eliminate array_keys():

Code:
foreach (get_object_vars($this) as $attribute => $val)
{
    if (is_object($this->$attribute))
    {
        $this->load->$attribute =& $this->$attribute;
    }
}
#3

[eluser]Derek Jones[/eluser]
Actually, since it's going to make a copy in PHP4 for the foreach, the use of array_keys() seems to be appropriate. ::grumbles at PHP4::
#4

[eluser]karloff[/eluser]
hate to be a pain, but i'm running php 4.4.8 and the bugs still appears.

if one of the admin's want to contact me and test on my site they are welcome to.

i need to upgrade to php5 asap!!
#5

[eluser]Derek Jones[/eluser]
What version of CI are you running, karloff?
#6

[eluser]karloff[/eluser]
version 1.0
#7

[eluser]Derek Jones[/eluser]
You'll need to update to 1.6.0 or greater. Changelog:

Quote:Fixed a bug (#3175) preventing certain libraries from working properly when autoloaded in PHP 4.
#8

[eluser]karloff[/eluser]
that's really changed. never took notice of the version i was using until now, however I only recently started using CI therefore I presumed I had a up-to-date version. must have accidentally pulled that out of the archives.


sorry for the silly question, and cheers for the heads up Derek
#9

[eluser]karloff[/eluser]
sorry to be a pain Derek, I noticed that i originally had version 1.6.1 not 1.0 as mentioned before. I have updated the necessary folders and still can pas the error data to the view.

i replaced the ;

* system/codeigniter
* system/database
* system/helpers
* system/language
* system/libraries
just did it a s quick fix live on the site, did i do anything wrong or miss anything?
#10

[eluser]Derek Jones[/eluser]
Ok if you are already running 1.6.1, it should have the fix. Open system/libraries/Controller.php and double check that the code is there. If it is, then please back up to your reproducible sample of code that is leading to this error.




Theme © iAndrew 2016 - Forum software by © MyBB