autoloader for loading classes won't work as expected - php

recently i started working with OOP and i created a classLoader to load the classes i use.. so i made this class and worked with it ( local ) and all went fine. But when i uploaded everything to my webhost it stopped working. i get the following error when i visit a page where the loader needs to load a class..
Fatal error: Uncaught exception 'Exception' with message 'Class "formhandler" could not be autoloaded from:
/var/www/vhosts/***.nl/httpdocs/admin/lib/formhandler.php' in
/var/www/vhosts/***.nl/httpdocs/admin/index.php:30 Stack trace:
#0 /var/www/vhosts/***.nl/httpdocs/admin/index.php(109): __autoload('formhandler')
#1 {main} thrown in /var/www/vhosts/***.nl/httpdocs/admin/index.php on line 30
the code for my autoloader is as followed..
function __autoload($className)
{
// get the base dir.
$base = dirname(__FILE__);
// get path
$path = $className;
$file = $base . "/lib/" . $path . '.php';
//if exists get file else throw error
if (file_exists($file))
{
require $file;
}
else
{
error_log('Class "' . $className . '" could not be autoloaded');
throw new Exception('Class "' . $className . '" could not be autoloaded from: ' . $file);
}
}

Compare the production path of the formhandler class. I guarantee there will be a difference between it and /var/www/vhosts/.nl/httpdocs/admin/lib/formhandler.php. Correct it.

i found the solution. i had new formhandler(); but instead i had to use new FormHandler(); in my script because my webhost didn;t find it.. verry annoying but it works now!

Related

PHP script can't work with autoloaded classes

The UsersView class object can't be loaded with the custom autoloader, finishing the PHP script with the following error. What would be the solution?
Fatal error: Uncaught Error: Class 'UsersView' not found in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php:4 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php on line 4
Project structure:
classes
|_____ UsersView.php
common
|_____ autoloader.php
|_____ registration
|_____ pendings.php
Pendings.php script
include("../autoloader.php");
$pendingsView = new UsersView();
$rows = $pendingsView->getAllUsers();
Autoloader.php script
spl_autoload_register('myAutoLoader');
function myAutoLoader($className) {
$path = "../classes";
$extension = ".php";
$fullPath = $path . $className . $extension;
if(!file_exists($fullPath)) {
return false;
}
include_once $fullPath;

ZF2 Uncaught Error:Class 'ArrayUtils' not found in E:\Temp\htdocs\zf-tutorial\public\index.php:43

I am new at Zend2 and i am following the Album tuturial on Zend. I get the following error:
Fatal error: Uncaught Error: Class 'ArrayUtils' not found in E:\Temp\htdocs\zf-tutorial\public\index.php:43 Stack trace: #0 {main} thrown in E:\Temp\htdocs\zf-tutorial\public\index.php on line 43
I can't find whats the problem, what do i do wrong? Do i miss some code? Or do i miss some rules in the tutorial?
here is my index.php file
<?php
use Zend\Mvc\Application;
/**
* Display all errors when APPLICATION_ENV is development.
*/
if ($_SERVER['APPLICATION_ENV'] === 'development') {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server') {
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (__FILE__ !== $path && is_file($path)) {
return false;
}
unset($path);
}
// Composer autoloading
include __DIR__ . '/../vendor/autoload.php';
if (! class_exists(Application::class)) {
throw new RuntimeException(
"Unable to load application.\n"
. "- Type `composer install` if you are developing locally.\n"
. "- Type `vagrant ssh -c 'composer install'` if you are using Vagrant.\n"
. "- Type `docker-compose run zf composer install` if you are using Docker.\n"
);
}
// Retrieve configuration
$appConfig = require __DIR__ . '/../config/application.config.php';
if (file_exists(__DIR__ . '/../config/development.config.php')) {
$appConfig = ArrayUtils::merge($appConfig, require __DIR__ . '/../config/development.config.php');
}
// Run the application!
Application::init($appConfig)->run();
To be able to use the Zend\Stdlib\ArrayUtils class, you have to either:
add a use statement at the top of your file: use Zend\Stdlib\ArrayUtils; or
change your ArrayUtils invocation to Zend\Stdlib\ArrayUtils

Autoload error in php

I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?

Implementing Interface and Autoloading Classes

I have a issue, i have the following interface (http://pastebin.com/c11xbdxh)
and i have the following class which implements the interface above (http://pastebin.com/m1zGNfSm).
I am using the following autoload function in order to load the classes dynamically:
function autoloadClass($className)
{
$classParts = explode("\\", $className);
$fileName = SYSTEM_CORE_PATH . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . strtolower(str_replace('_', DIRECTORY_SEPARATOR, end($classParts)) . '.class.php');
if (is_readable($fileName)) {
if (SYSTEM_DEBUG) {
include_once($fileName);
} else {
#include_once($fileName);
}
}
}
spl_autoload_register("autoloadClass");
and when i creating a new object class (under the autoloading code) i don't get any error neither any output...
try {
$db = new Core\Infrastructure\MySQL(array('user' => DB_USER, 'pass' => DB_PASS, 'host' => DB_HOST, 'name' => DB_NAME));
} catch (PDOException $pdoE) {
echo $pdoE->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
echo "<pre>ddd";
$db->runQuery("SELECT * FROM `users`;");
print_r( $db->fetchData());
Thanks for your kind help :)
"Don't get any error neither any output" usually means a Fatal or Parse error eaten by error_reporting settings. Check logs. Make sure error_reporting(E_ALL) is set, preferrably in ini file.
Add debug output when $fileName is not readable. This will likely provide an insight.
Ok i've fixed it, the autoloading only loads the class from the file, it does'nt load the interface.
i've simply added a short code based on class_implements function to the autoload function i wrote.
thanks for all of your help :D

What's wrong with my autoloader?

I'm trying to register a few autoloaders and I get an HTTP 500. My error log says the following:
[05-Aug-2013 04:32:38 UTC] PHP Fatal error: Uncaught exception
'LogicException' with message 'Function 'Autoloader::config' not
callable (non-static method Autoloader::config() should not be called
statically)' in /home2/canforce/public_html/index.php:5
There was some stack trace part on the end of the error log, but it came showed up in huge letters so I took it out, I didn't think it was important.
I think my autoloader should work based on what I've read but for some reason it doesn't, here's the code:
index.php
include("config/autoloader.php");
spl_autoload_register('Autoloader::config');
spl_autoload_register('Autoloader::controller');
spl_autoload_register('Autoloader::service');
config/autoloader.php
class Autoloader {
function config($class) {
$file = 'config/' . $class . '.php';
if(file_exists($file)) {
require_once $file;
}
}
function controller($class) {
$file = 'presentation/controllers/' . $class . '.php';
if(file_exists($file)) {
require_once $file;
}
}
function service($class) {
$file = 'model/services/' . $class . '.php';
if(file_exists($file)) {
require_once $file;
}
}
}
You need to create an instance of the Autoloader class and then pass it to the register function.
include("config/autoloader.php");
$autoloader = new Autoloader();
spl_autoload_register(array($autoloader, 'loader'));

Categories