Fatal error: Class 'adLDAP' not found - php

The file I'm viewing is called examples.php and contains the following code:
include (dirname(__FILE__) . "/adLDAP.php");
try {
$adldap = new adLDAP();
}
The error that displays reads:
Fatal error: Class 'adLDAP' not found in /var/www/examples.php on line 14
Line 14 is:
$adldap = new adLDAP();
adLDAP.php is in the same folder as examples.php and contains the adLDAP class.
Have I messed up my include statement? I get "no such file or directory" with the other formats I have tried. Feels like I'm missing something obvious.
adLDAP.php instantiates the adLDAP class early on:
<?php
namespace adLDAP;
require_once(dirname(__FILE__) . '/collections/adLDAPCollection.php');
require_once(dirname(__FILE__) . '/classes/adLDAPGroups.php');
require_once(dirname(__FILE__) . '/classes/adLDAPUsers.php');
require_once(dirname(__FILE__) . '/classes/adLDAPFolders.php');
require_once(dirname(__FILE__) . '/classes/adLDAPUtils.php');
require_once(dirname(__FILE__) . '/classes/adLDAPContacts.php');
require_once(dirname(__FILE__) . '/classes/adLDAPExchange.php');
require_once(dirname(__FILE__) . '/classes/adLDAPComputers.php');
class adLDAP {
etc.

adLDAP class definition is in adLDAP namespace. You need to tell PHP that this class is in this namespace:
$adldap = new adLDAP\adLDAP();
You can also import this class with use keyword:
<?php
include (dirname(__FILE__) . "/adLDAP.php");
use adLDAP\adLDAP;
$adldap = new adLDAP;

Related

PHP autoload can't find the class in php project

Project file structure
I'm getting
127.0.0.1:52368 [500]: GET /controllers/login - Uncaught Error: Class "App\Core\Database" not found in /path/to/my/Php/project/src/controllers/index.php:9
Stack trace:
#0 {main}
thrown in /path/to/my/Php/project/src/controllers/index.php on line 9
and the class firing an error
<?php
namespace App\controllers;
use App\Core\Database;
//$conn = Database::getConnection();
try {
$conn = new Database(__DIR__ . './../config.php');
} catch (\Exception $e) {
}
$query = "SELECT id, username, image, aboutme
FROM users
LEFT JOIN persons on users.id = persons.user_id
LIMIT 24";
$tbh = $conn->prepare($query);
$tbh->execute();
$persons = $tbh->fetchAll();
require_once basename("/") . 'views/index.view.php';
also my autoload_classmap.php
return array(
'App\\Core\\Database' => $baseDir . '/src/Core/Database.php',
'App\\Core\\DotEnv' => $baseDir . '/src/Core/DotEnv.php',
'App\\Core\\Router' => $baseDir . '/src/Core/Router.php',
'App\\Entity\\Person' => $baseDir . '/src/Entity/Person.php',
'App\\Entity\\User' => $baseDir . '/src/Entity/User.php',
And I'm launching server from src directory :
php -S localhost:8000
Any help would be appreciated. Thank you.
Edit ...
I'm calling index.php from url : localhost:8000 and even with this code in index.php
<?php
namespace App;
//use App\core\Database;
use App\Core\Router;
require 'functions.php';
//$config = require_once ('config.php');
//$db = new Database($config);
//require 'Entity/User.php';
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$router = new Router();
$router->getRoute($uri);
//require 'router.php';
I'm getting
127.0.0.1:53738 [500]: GET / - Uncaught Error: Class "App\Core\Router" not found in
You must create a class and add data inside it .
prepare($query);
$tbh->execute();
$persons = $tbh->fetchAll();
require_once basename("/") . 'views/index.view.php';
}

run script in background(shell_exec) fails on composer/PHP-DI function

I run that script manually and everything works,
$container = require '../config/bootstrap.php';
try {
$test = $container->get(Test::class);
$test->run();
}
catch(\Exception $e) {
echo $e->getMessage() . "\n";
die;
}
but when I call that file from another folder like the following :
$script = __DIR__ .'/folder/test-service.php';
$output = shell_exec('php '.$script);
var_dump($output);
I get the following error:
Fatal error: require(): Failed opening required '../config/bootstrap.php' (include_path='.:') in /anotherFolder/folder/test-service.php on line 13
and also after that, I got a lot of error that files do not exist.
bootstrap.php include the class mapping of PHP-DI
require '../vendor/autoload.php';
use DI\ContainerBuilder;
$containerBuilder = new ContainerBuilder;
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
$container = $containerBuilder->build();
return $container;
The require instruction is relative to your current directory.
You could replace the line with require __DIR__.'/../config/bootstrap.php';

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?

autoloader for loading classes won't work as expected

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!

Categories