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;
Related
My code, script start.php in app folder:
require __DIR__ . '../../dropbox-php-sdk-master/vendor/autoload.php';
session_start();
$_SESSION['user_id'] = 1;
$dropboxKey = '';
$dropboxSecret = '';
$appName = '';
$appInfo = new Dropbox\AppInfo($dropboxKey,$dropboxSecret);
My folder structure:
1.app
1.1. start.php
2.dropbox-php-sdk-master
2.1.vendor
2.1.1. autoload.php
3.index.php (calls function start.php)
And when I call the index.php i recive this erro:
Fatal error: Uncaught Error: Class 'Dropbox\AppInfo' not found in C:\xampp\htdocs\tutorials\dropboxapi\app\start.php:12 Stack trace: #0 C:\xampp\htdocs\tutorials\dropboxapi\index.php(3): require() #1 {main} thrown in C:\xampp\htdocs\tutorials\dropboxapi\app\start.php on line 12
I follow some tutorials on the internet and they all have the same structure, what Im doing bad?
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
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?
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'));
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!