Welcome Guest, Not a member yet? Register   Sign In
spl_autoload_register Throws an Error in My Production Server
#1

[eluser]fjamal[/eluser]
My application is working fine in development environment. Once i uploaded it to my production server, i got the following error:


Quote:Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home/fjamal/public_html/*******/anyname/application/config/config.php on line 27

The errors refers to the following code:

Code:
spl_autoload_register(function($class)
{
if (strpos($class, 'CI_') !== 0)
{
  if (file_exists($file = APPPATH . 'core/' . $class . EXT))
  {
   include $file;
  }
  elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
  {
   include $file;
  }
}
});

If i change the above code to the older version as:

Code:
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
  if (file_exists($file = APPPATH . 'core/' . $class . EXT))
  {
   include $file;
  }
  elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
  {
   include $file;
  }
}
}
I get the following error:

Quote:Fatal error: Class 'Frontend_Controller' not found in /home/fjamal/public_html/********/anyname/application/controllers/home.php on line 4

Explanation of the error: my controller extends from Frontend_Controller which resides in Libraries folder. Frontend_Controller extends from MY_Controller which resides under core folder. For some reason, all these problems in the production environment, i don't get it in my localhost. Hence, home is default controller.

I tried my best, but couldn't figure it out. Any help will be appreciated.
#2

[eluser]Aken[/eluser]
First, what version of PHP is your production running? The anonymous function you're using is supported in 5.3+

The second error (Frontend_Controller not found) could be your autoload function not correctly loading the files necessary (which is just a guess). You might consider adding an include / require call directly in your MY_Controller file, or even add those classes to the file (you can add whatever classes you want, doesn't have to be only one - it's just normal PHP code here). You actually don't even need to name your class MY_Controller - just the file needs to be named that.
#3

[eluser]fjamal[/eluser]
[quote author="Aken" date="1350012919"]First, what version of PHP is your production running? The anonymous function you're using is supported in 5.3+

The second error (Frontend_Controller not found) could be your autoload function not correctly loading the files necessary (which is just a guess). You might consider adding an include / require call directly in your MY_Controller file, or even add those classes to the file (you can add whatever classes you want, doesn't have to be only one - it's just normal PHP code here). You actually don't even need to name your class MY_Controller - just the file needs to be named that.[/quote]

My production server has PHP Version 5.2.17. Development environment has PHP 5.3.1.

Quote:The second error (Frontend_Controller not found) could be your autoload function not correctly loading the files necessary (which is just a guess).

If that was the case, it wouldn't work in localhost.

Quote:You might consider adding an include / require call directly in your MY_Controller file, or even add those classes to the file (you can add whatever classes you want, doesn't have to be only one - it's just normal PHP code here).

Will try it and see how it goes.

Cheers
#4

[eluser]Aken[/eluser]
Just because it works in one environment doesn't guarantee it'll work in another. Maybe there's a path not being set correctly, or an issue of case (in)sensitivity. Unless you can guarantee that the environments are the same, you risk having bugs like this sometimes. But the error is saying that Frontend_Controller does not exist, which means the file was not correctly included, which leads me to believe that the autoload function didn't work as expected. Do some debugging.

And that definitely explains your first error, so yeah. Upgrade your PHP version if you can, because anonymous functions are awesome.
#5

[eluser]fjamal[/eluser]
I figured it out.

Code:
Fatal error: Class ‘Frontend_Controller’ not found in /home/fjamal/public_html/********/anyname/application/controllers/home.php on line 4

My Frontend controller file was named as Frontend_controller.php and the class was named as Frontend_Controller. The interpreter was looking for Frontend_Controller.php rather than Frontend_controller.php. Solution: renamed my file to Frontend_Controller.php

Now getting another error even though i have a session table:
Code:
Fatal error: Call to a member function num_rows() on a non-object in /home/fjamal/public_html/********/system/libraries/Session.php on line 216

Will try to figure it out. Your suggestions won't harm.

My DB credentials are ok.

Cheers
#6

[eluser]Aken[/eluser]
num_rows() needs to be called on a query result. One of two things is likely: A) Your query is improperly formatted and/or failed, so it returned something other than a standard result object; B) You retrieved a result row/set (such as result(), row(), etc.) and you're trying to use num_rows() on that by mistake.
#7

[eluser]fjamal[/eluser]
After turning the debugger, i get this message:

Unable to select the specified database: ****
Filename: core/Loader.php
Line Number: 346

I forgot to assign the username to the DB. Probably, i need some rest. Problem is solved


Thanks for your prompt support.




Theme © iAndrew 2016 - Forum software by © MyBB