Fatal error: Uncaught LogicException: Function autooad not found - php

I would know if there something speficic in php 7.3.3-1+0~20190307202245.32+stretch~1.gbp32ebb2
because I have this error when I try to install a software.
:autoload' not found (class 'Core\\OM\\CORE' not found)
Got error 'PHP message: PHP Fatal error: Uncaught LogicException: Function 'Core\\OM\\CORE::autoload' not found (class 'Core\\OM\\CORE' not found) in /var/www/.........../application.php:21\nStack trace:\n#0 /var/www/........./application.php(21): spl_autoload_register('Core\\\\OM...')\n#1 /var/www/clients/........./install/index.php(12): require('/var/www/client...')\n#2 {main}\n thrown in /var/www/........../install/includes/application.php on line 21'
On my localhost under linux, I have not this problem and everything works fine.
php : PHP Version 7.3.2-3
Thank you
For example in install.php directory :
use Core\OM\CORE;
use Core\OM\HTML;
// set the level of error reporting
error_reporting(E_ALL & ~E_DEPRECATED);
define('CORE_BASE_DIR', realpath(__DIR__ . '/../../includes/') . '/Core/');
require(CORE_BASE_DIR . 'OM/CORE.php');
spl_autoload_register('Core\OM\CORE::autoload')
about CORE.PHP
public static function autoload($class) {
$prefix = 'Core\\';
if (strncmp($prefix, $class, strlen($prefix)) !== 0) {
return false;
}
if (strncmp($prefix . 'OM\Module\\', $class, strlen($prefix . 'OM\Module\\')) === 0) { // TODO remove and fix namespace
$file = dirname(CORE_BASE_DIR) . '/' . str_replace(['Core\OM\\', '\\'], ['', '/'], $class) . '.php';
$custom = dirname(CORE_BASE_DIR) . '/' . str_replace(['Core\OM\\', '\\'], ['Core\Custom\OM\\', '/'], $class) . '.php';
} else {
$file = dirname(CORE_BASE_DIR) . '/' . str_replace('\\', '/', $class) . '.php';
$custom = str_replace('Core/OM/', 'Core/Custom/OM/', $file);
}
if (is_file($custom)) {
require($custom);
} elseif (is_file($file)) {
require($file);
}
}

Related

Error: __autoload() is deprecated, use spl_autoload_register() instead [duplicate]

This question already has an answer here:
How to use spl_autoload() instead of __autoload()
(1 answer)
Closed 1 year ago.
I am facing a PHP error on live server. I believe it is a version issue.
The error is included below and occurs in the config.php file:-
ERROR: __autoload() is deprecated, use spl_autoload_register() instead.
Code snippet from config.php file
if (!function_exists('__autoload')) {
function __autoload($class) {
if (strpos($class, 'Auth_Controller') === 0) {
#include_once( APPPATH . 'core/' . $class . EXT );
}
if (strpos($class, 'Rest_Controller') === 0) {
#include_once( APPPATH . 'core/' . $class . EXT );
}
}
}
Use spl_autoload_register to add a class loader function.
It is also a good practice to end the function just after finding the class.
$autoload = function ($class) {
if (strpos($class, 'Auth_Controller') === 0) {
#include_once( APPPATH . 'core/' . $class . EXT );
return;
}
if (strpos($class, 'Rest_Controller') === 0) {
#include_once( APPPATH . 'core/' . $class . EXT );
return;
}
};
spl_autoload_register($autoload);

Recursive copy directories

I have a function for recursive copy directories. But I'm getting error:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/temp/install_5630013a79723/site, /temp/install_5630013a79723/site): The system cannot find the file specified. (code: 2)'
public static function copyDir($source, $dest) {
#mkdir($dest, 0755);
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
if ($item->isDir()) {
#mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
#copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
}
How can I fix/improve my function?

PHP autoloader class to load files from two or three different path [duplicate]

This question already has answers here:
Best Way To Autoload Classes In PHP
(8 answers)
Closed 1 year ago.
I'm learning to use php autuoloader...
As much as I understand, we can use __autoloader or spl_autoloader_* to auto load files.
Assume this is my directories structure :
ROOT
|
|
ADMIN
| |
| |
| DIST
| |
| SOME_FOLDER
| SOME_FOLDER
| TPL
| |
| |
| SOME_FOLDER1
| |
| test.php
| SOME_FOLDER2
| |
| example1.php
| example2.php
| example3.php
|
|
CLASSES
|
basics.php
class1.php
class2.php
class3.php
class4.php
|
index.php
I made this class for autoload files in CLASSES directory :
basics.php :
class MyAutoLoader
{
public function __construct()
{
spl_autoload_register( array($this, 'load') );
}
function load( $file )
{
//spl_autoload( 'load1' );
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
require( $file . '.php' );
}
}
and in index.php I will include basics.php and every thing is fine for files are stored in CLASSES folder...
require_once("CLASSES/basics.php");
$loaderObject = new MyAutoLoader();
with this code, I can declare class1 ... class3
Now I want to have an autoloder that could be load files in SOME_FOLDER2 which in this case are example1.php , example2.php and example3.php
I tried some cases but the files in SOME_FOLDER2 won't be load using autoloader.
My attempts :
I made a function named load2 in MyAutoLoader class that try to include files from SOME_FOLDER2
function load2( $file )
{
//spl_autoload_register('load2');
echo 'Inside LOADER2 ' . __METHOD__ . '<br>';
require ( 'ADMIN/TPL/' . $file . '.php' );
}
And I changed spl_autoload_register in MyAutoLoader constructor :
$allMethods = get_class_methods( 'MyAutoLoader' );
$allMethods = array_splice( $allMethods, 1 );
foreach( $allMethods as $method )
{
spl_autoload_register( array($this, $method) );
}
But none of them didn't work for me...
Would you please tell me what's wrong in my code or what is my misunderstanding about auloader?
Thanks in Advance
I think your problem basically boils down to not checking if the file exists before requireing it... that will produce a fatal error if the file doesn't exist in the first folder that you attempt to include from.
I don't know your use case, but here are some suggestions:
Can you just use a single autoloader?
function my_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
} else if (is_file('ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php')) {
require_once 'ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php';
}
}
spl_autoload_register("my_autoload");
Or if you need to declare them independently (ie. two or more autoloaders):
function classes_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
}
}
spl_autoload_register("classes_autoload");
function admin_autoload($class_name) {
if (is_file('ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php')) {
require_once 'ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php';
}
}
spl_autoload_register("admin_autoload");
Or make your autoloader class generic:
class MyAutoLoader {
private $path;
public function __construct($path) {
$this->path = $path;
spl_autoload_register( array($this, 'load') );
}
function load( $file ) {
if (is_file($this->path . '/' . $file . '.php')) {
require_once( $this->path . '/' . $file . '.php' );
}
}
}
$autoloader_classes = new MyAutoLoader('CLASSES');
$autoloader_admin = new MyAutoLoader('ADMIN/TPL/SOME_FOLDER2');
If you don't want to manually keep the list of child folders inside ADMIN/TPL up to date you could even do something like this to autoload from any of them (this obviously assumes that all subfolders of ADMIN/TPL contains classes):
function my_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
} else {
$matching_files = glob('ADMIN/TPL/*/' . $class_name . '.php');
if (count($matching_files) === 1) {
require_once $matching_files[0];
} else if (count($matching_files) === 0) {
trigger_error('Could not find class ' . $class_name . '!', E_USER_ERROR);
} else {
trigger_error('More than one possible match found for class ' . $class_name . '!', E_USER_ERROR);
}
}
}
spl_autoload_register("my_autoload");
Your problem may actually be as simple as this:
Your basics.php is inside the folder CLASSES, so when you include or require files from within basics.php it starts in that folder.
So if you want to include files from ADMIN/TPL/SOME_FOLDER2 you actually need to "jump" up one level first, ie require '../ADMIN/TPL/SOME_FOLDER2/' . $file . '.php';
To avoid this kind of confusion I would recommend adding a constant to the beginning of index.php like this:
define('BASEPATH', __DIR__);
...and then prepending that to all require and include statements - ie:
class MyAutoLoader {
public function __construct() {
spl_autoload_register( array($this, 'load') );
spl_autoload_register( array($this, 'load2') );
}
function load( $file ) {
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
$classfile = BASEPATH . '/CLASSES/' . $file . '.php';
if ( is_file( $classfile ) ) {
require( $classfile );
}
}
function load2( $file ) {
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
$classfile = BASEPATH . '/ADMIN/TPL/SOME_FOLDER2/' . $file . '.php';
if ( is_file( $classfile ) ) {
require( $classfile );
}
}
}

Class not found in Bootstrap.php in CLI mode (Zend)

I am getting an error when trying to run app in CLI mode. First of all I have daemon controller which allows to run daemon instances (objects with only one required method 'run' derived from my daemon interface) using url /daemon-cli/:Daemon_Class_To_Run. So there is nothing interesting from this side.
Also I have little sh script to run this controller in a simplified way like this:
# ./daemon.sh Ami_Daemon_EventParser
This returns me the next error:
<?
class System_Controller_Plugin_ModuleLayoutLoader extends Zend_Controller_Plugin_Abstract {
/**
* Array of layout paths associating modules with layouts
*/
protected $_moduleLayouts;
// here was the full code of this class
}
PHP Fatal error: Class 'System_Controller_Plugin_ModuleLayoutLoader' not found in /var/www/htdocs/application/Bootstrap.php on line 99
System namespace is located in application/library/System and this is just a set of toolkits and libraries. That is completely weird because it works well in apache2handler mode, but crashes in cli. I don't understand how the class cannot be found if the code of "not founded" class are returned in the error. I suppose I bootstrapped application in a wrong way.
Some of bootstrap code:
protected function _initAppModules()
{
$modules = array();
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('System_');
$includePath = '';
// Add model directories to include path
if ($dhApp = opendir(APPLICATION_PATH . '/modules')) {
while (($dirApp = readdir($dhApp)) !== false) {
if (substr($dirApp, 0, 1) != '.' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp)) {
$modules [] = $dirApp;
if ($dhModule = opendir(APPLICATION_PATH . '/modules/' . $dirApp)) {
while (($dirModule = readdir($dhModule)) !== false) {
if ($dirModule == 'library' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp . '/library')) {
$includePath .= PATH_SEPARATOR . APPLICATION_PATH . '/modules/' . $dirApp . '/library';
$autoloader->registerNamespace(ucfirst($dirApp));
}
}
closedir($dhModule);
}
}
}
closedir($dhApp);
}
ini_set('include_path', ini_get('include_path') . $includePath);
return $modules;
}
protected function _initResources()
{
Zend_Controller_Action_HelperBroker::addPrefix('System_Controller_Action_Helper');
$resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH . '/library/System', 'namespace' => ''));
$resourceLoader->addResourceTypes(array('form' => array('path' => 'Form/Resource/', 'namespace' => 'Form'), 'model' => array('path' => 'Model/Resource/', 'namespace' => 'Model')));
System_API_Abstract::load();
return $resourceLoader;
}
protected function _initView(array $options = array())
{
$this->bootstrap('AppModules');
$this->bootstrap('FrontController');
$view = new Zend_View();
$view->addScriptPath(APPLICATION_PATH . '/views/scripts');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/modules/gui/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/library/System/View/Helper', 'System_View_Helper');
// next line triggers the error
$layoutModulePlugin = new System_Controller_Plugin_ModuleLayoutLoader();
foreach ($this->getResource('AppModules') as $module) {
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/layouts')) {
$layoutModulePlugin->registerModuleLayout($module, APPLICATION_PATH . '/modules/' . $module . '/views/layouts');
}
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/helpers')) {
$view->addHelperPath(APPLICATION_PATH . '/modules/' . $module . '/views/helpers', ucfirst($module) . '_View_Helper');
}
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
Zend_Layout::startMvc(array('layoutPath' => '../application/views/layouts', 'layout' => 'layout'));
$this->getResource('FrontController')->registerPlugin($layoutModulePlugin);
return $view;
}
And in the index.php I bootstrap all modules.
if (php_sapi_name() == 'cli') {
$_SERVER['REQUEST_URI'] = $argv[1];
$_SERVER ['HTTP_HOST'] = 'mydomain.info';
ini_set('session.use_cookies', false);
$application->bootstrap()->run();// in case if I find the way to bootstrap partially
} else {
$application->bootstrap()->run();
}
I am fighting with this for a couple of weeks, so I hope somebody has an answer or a good advice. Any help is greatly appreciated. Thanks!
Finally I did that!
As you can see I had a php short tag in the error output. Php often uses separate php.ini for cli mode as my rackspace distributive does. Php distros for windows usually have the common ini file.

php - Class 'Browscap' not found in

I'm using Browscap downloaded from Github.
So I have a _class.php
require_once dirname(__FILE__) . "/core/CORE.php";
require_once dirname(__FILE__) . "/core/Browscap.php";
// use phpbrowscap\Browscap;
class _class extends CORE {
//put your code here
public function __construct($mysql_setup = null) {
parent::__construct($mysql_setup);
$bc = new Browscap(dirname(__FILE__) . "/../cache"); //this is the error line
$get_browser = $bc->getBrowser(null, true);
$get_browser["HTTP_REFERER"] = $_SERVER["HTTP_REFERER"];
$get_browser["REQUEST_TIME"] = date("Y-m-d H:i:s", $_SERVER["REQUEST_TIME"]);
$this->dbCreateTable();
$user = $_SERVER['REMOTE_ADDR'] . ";" . session_id();
if ($this->insertUser($user) !== false) {
$http_user_id = $this->dbLastInsertID();
foreach ($get_browser as $k => $v) {
if (($type_id = $this->getHttpUserTypeDB($k)) !== false) {
$this->dbInsert("http_user_agent_infos", array("info" => $v, "http_user_agent_id" => $http_user_id, "http_user_agent_type_id" => $type_id));
}
}
}
}
My directory hierarchy is like this
C:\xampp\htdocs\test
classes(folder)
core(folder)
Browscap.php
CORE.php
_class.php
form.php
cache(folder)
browscap.ini
cache.php
php_browscap.ini
The error is
Fatal error</b>: Class 'Browscap' not found in <b>C:\xampp\htdocs\job6b\classes\_class.php</b> on line <b>28</b><br />
I'm not sure where I have my error...or did I put the path wrongly?
Thanks

Categories