Composer Fatal error: Cannot redeclare composerRequire - php

When running the PHP Development server on my MacOS I get the following error from composer.
Fatal error: Cannot redeclare composerRequire01a3dc0b1f885ed2b645be37711584c1()
(previously declared in /Users/<user>/Desktop/<app>/vendor/composer/autoload_real.php:63)
in /Users/<user>/Desktop/<app>/vendor/composer/autoload_real.php on line 70
However when running this exact same code on my Windows box everything seems to run perfectly fine. I've verified that I am not including the require statement for the autoload more than once. What could be causing this? I'm using the Composer from HomeBrew if that makes any difference. I just can't see how such a specific file namespace as that could be getting included multiple times.
EDIT
Composer autoload_real.php
<?php
// autoload_real.php #generated by Composer
class ComposerAutoloaderInit01a3dc0b1f885ed2b645be37711584c1
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit01a3dc0b1f885ed2b645be37711584c1', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit01a3dc0b1f885ed2b645be37711584c1', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit01a3dc0b1f885ed2b645be37711584c1::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit01a3dc0b1f885ed2b645be37711584c1::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire01a3dc0b1f885ed2b645be37711584c1($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire01a3dc0b1f885ed2b645be37711584c1($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}

It seems you made a copy of an existing project directory and try to tun it in a new directory
Try to delete composer.lock and vendor/ and run composer update
I'm not 100% sure but hope it will help

Related

Cannot declare class ComposerAutoloaderInit

I have a question i have an error im not enabled to fix. The error is saying the name is already in use.
What im trying to do is im calling a API that needs to give some response back. Because of this error im not enabled to acces the data of my API
The Error
Cannot declare class ComposerAutoloaderInitd1e500cc63b56a87596c43fefa8d9495, because the name is already in use in
This is my autoload.php
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd1e500cc63b56a87596c43fefa8d9495::getLoader();
autoload_real.php
<?php
// autoload_real.php #generated by Composer
class ComposerAutoloaderInitd1e500cc63b56a87596c43fefa8d9495
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitd1e500cc63b56a87596c43fefa8d9495', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitd1e500cc63b56a87596c43fefa8d9495', 'loadClassLoader'));
$includePaths = require __DIR__ . '/include_paths.php';
$includePaths[] = get_include_path();
set_include_path(implode(PATH_SEPARATOR, $includePaths));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitd1e500cc63b56a87596c43fefa8d9495::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}
I had this issue when including two composer autoloaders from two directories where one originated as a copy of the other one (don't ask me why).
Based on this GitHub Issue I concluded my solution:
Delete the composer lock-file (e.g. composer.lock) and execute composer update.
In my case I just deleted the vendor-directory too.

PHP Fatal error: Class 'PayAssure\Models\User' not found

please l'm seeing this error if a user login to app after uploading my app to server. domain.com is currently unable to handle this request. http error 500 And everything was working fine in my local xampp server. When i checked my error-log, i saw this
PHP Fatal error: Class 'PayAssure\Models\User' not found in /home/payassur/public_html/dashboard/app/init.php on line 22.
What could have happened? please help.
Below is my init.php code.
<?php
session_start();
define('ABSPATH', dirname(__FILE__) . '/');
require_once (ABSPATH . "../vendor/autoload.php");
$GLOBALS['config'] = [
'mysql' => [
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db' => 'payassure'
]
];
if (!$_SESSION['user_id']) {
header ("Location: ../index.php");
}
use PayAssure\Models\User;
use PayAssure\Models\Database;
$u = new User; //this is the line 22
$dd = new Database;
$dbb = $dd->getConnection();
$userIsBlocked = $dbb->prepare("SELECT * FROM users WHERE id = :user_id AND
blocked = '1'");
$userIsBlocked->execute(['user_id' => $_SESSION['user_id']]);
if ($userIsBlocked->rowCount() > 0) {
header("Location: userBlocked.php");
}
This is the code in models/user
<?php
namespace PayAssure\Models;
use PayAssure\Models\Database;
/**
* #package PayAssure HQ
*/
class User
{
protected $db;
public $errors;
public function __construct()
{
$d = new Database;
$this->db = $d->getConnection();
return $this->db;
}
my vendor/autoload
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit3a313f3d3ce53ad02ae8da633a01a1c9::getLoader();
my autoload_real.php
<?php
// autoload_real.php #generated by Composer
class ComposerAutoloaderInit3a313f3d3ce53ad02ae8da633a01a1c9
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array(
'ComposerAutoloaderInit3a313f3d3ce53ad02ae8da633a01a1c9',
'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array(
'ComposerAutoloaderInit3a313f3d3ce53ad02ae8da633a01a1c9',
'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION');
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(
\Composer\Autoload\ComposerStaticInit3a313f3d3ce53ad02ae8da633a01a1c9::
getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}
My composer.json file
{
"autoload": {
"psr-4": {
"PayAssure\\": "app/PayAssure"
}
},
"require-dev": {
"heroku/heroku-buildpack-php": "*",
"phpdocumentor/phpdocumentor": "2.*"
}
}
Try this
use PayAssure\Models\User as user1;
use PayAssure\Models\Database;
$u = new user1; //this is the line 22
Then see still error in 22 no line.

PHP autoloader issue

I have a very old application script that actually loads controller, model and entity with the class name using below snippet
function __autoload($className)
{
list($filename , $suffix) = explode('_' , $className);
switch (strtolower($suffix))
{
case 'model':
$folder = '/model/';
$suffix = BIZ_SUFFIX;
break;
case 'dao':
$folder = '/entity/';
$suffix = DAO_SUFFIX;
break;
}
$file = SITE_PATH . $folder . strtolower($filename) . $suffix .'.php';
if (file_exists($file))
{
include_once($file);
}
else
{
die("File '$filename' containing class '$className' not found in '$folder'.");
}
}
Now I have problem in integrating any third party autoloaders, even with the composer autoload.
I have even tried below methods as well
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
and
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
Error msg:
Fatal error: Class 'Template_Model' not found
If you use spl_autoload_register, the __autoload function will not be called.
Rename __autoload to something else, ex. old_autoload. And register it in autoloaders spl_autoload_register('old_autoload');

Allow SVN commit with existing PreCommit hook warnings

I am using SVN precommit hooks to validate my code standard (PSR2) before being able to commit. This works perfectly with just one exception. My unit test (PHPUnit) files exist of my bootstrap class existing of all static unit test functions, but also enables error messages above the bootstrap class definition.
The PSR2 standard will give a warning when trying to commit this, because you cannot have code that is not in a class or function in a file that contains a class definition.
Does anyone have either a way to exclude this error in my codesniffer or a way to make my code valid (without putting the code to enable my error messages in each static function of the bootstrap class)?
Here's the file:
<?php
namespace AlbumTest;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;
error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);
class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;
public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/TestConfig.php')) {
$testConfig = include __DIR__ . '/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
}
$zf2ModulePaths = array();
if (isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath)) ) {
$zf2ModulePaths[] = $path;
}
}
}
$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS')
? ZF2_MODULES_TEST_PATHS : '');
static::initAutoloader();
// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => array(
'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
),
);
$config = ArrayUtils::merge($baseConfig, $testConfig);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
static::$config = $config;
}
public static function getServiceManager()
{
return static::$serviceManager;
}
public static function getConfig()
{
return static::$config;
}
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH')
? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library')
? $vendorPath . '/ZF2/library' : false));
if (!$zf2Path) {
throw new RuntimeException(
'Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'
);
}
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
}
AutoloaderFactory::factory(
array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
)
);
}
protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
How does your precommit-hook look like? Could you just remove the first few lines dynamically before sending the to the Codesniffer?
Another solution would be to set the error_reporting in the init of your bootstrap

php namespace and autoload

I have some class
/library/QPF/Loader.php
namespace QPF;
class Loader
{
protected static $loader = null;
public function __construct()
{
spl_autoload_register('QPF\Loader::_autoload');
}
public static function init()
{
if (null === self::$loader) {
self::$loader = new Loader();
}
return self::$loader;
}
public function _autoload($class)
{
//if (class_exists($class)) return true;
$classFile = str_replace('\\', '/', $class) . '.php';
require_once $classFile;
if (!class_exists($class)) throw new Extension('Not found class');
}
}
/library/Version.php
namespace QPF;
class Version
{
public function getVersion()
{
return '0.1';
}
}
/public/index.php
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../library');
define('APPLICATION_PATH', dirname(__FILE__) . '/../application');
require_once 'QPF/Loader.php';
QPF\Loader::init();
echo 'start';
use QPF;
$v = new QPF\Version();
var_dump($v);
echo 'ss';
Version class loading, but var_dump show what it's empty class without function getVersion();
startobject(QPF\Version)#2 (0) { } ss
Methods do not show up in var_dump or print_r output, as they are not part of the state of the object. Try calling the method; it should work as expected.

Categories