Drupal 8 not adding my module class to the registry / autoloading - php

I'm having trouble getting Drupal 8 to load my custom module's classes from the same namespace. Here's what I've got:
<?php
namespace Drupal\sign_up\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
// ... snip ...
class StandardForm extends CLESignUpForm
{
Which all works perfectly well on my local environment. When deployed the server says: PHP Fatal error: Class 'Drupal\sign_up\Form\CLESignUpForm' not found in /path/to/drupal/modules/sign_up/src/Form/StandardForm.php on line 21
If I add the line:
include 'CLESignupForm.php';
It works just fine. But that defeats the purpose of auto-loading classes, doesn't it? get_declared_classes() does not show my base class .. or even the currently loaded class for that matter.
Any thoughts on how to make this particular environment comply? I've already loaded the db and config directly from my local setup where it's working. Could it be a php / apache setting somehow?

Turns out I'm a knucklehead. CLESignUpForm is the incorrect capitalization of the type. The correct capitalization is CLESignupForm. Still not clear why it works in every other environment and not this one, but it makes me feel like there's a bit more sanity at least.

Related

Class not found after "require_once"

I get the following error:
`Fatal error: Class 'DummyClass' not found in...`
<?php
require_once("3rdparty/simplesaml/lib/_autoload.php");
class login extends DummyClass { (this is the line the error refers to)
[...]
}
?>
If I comment out the require_once it works perfectly fine.
DummyClass is defined externally and can be found in the prepend-file. (I don't think it matters for this problem as it works as expected if I comment out require_once)
The path to the file should also be correct as it gives me a "Failed opening required..." Error if I change the path.
I also tried switching between PHP 5.6 and 7 - no difference.
So, I would like to ask you for help. Do you have any hints / ideas, why I might get that error?
Problem solved.
The old framework was using the old __autoload function, which is deprecated.
SimpleSAMLPHP used the new function. Those autoload-combinations cause one of them to override the other.
Solution:
Switch from __autoload to spl_autoload_register.
Similar Question: Override vendor autoload composer

php, laravel, log4php: Class 'Apache\Log4php\Hierarchy' not found error

I'm new to php and i'm using log4php with laravel.
My project structure is
->Root
->Laravel
->app
->folderx
->abc.php
->otherfolders
.
.
->vendor
->composer.json (contains log4php and laravel)
->logconfig.xml
I'm trying to initialize the logger from inside abc.php,
Logger::configure('../../../logconfig.xml');
but it gives the error message
Class 'Apache\Log4php\Hierarchy' not found
I verified that the class Hierarchy.php exists in the vendor/apache/log4php/src under Root folder. Also, if I open Logger.php and go to the line where Hierarchy in initialized and ctrl+click(in eclipse) on Hierarchy, it takes me to Hierarchy.php.
I'm trying to figure out why php is not able to find that class.
Any help/suggestions would be greatly appreciated.
Thanks
That error message shows that your PHP is looking for a namespaced class. Log4PHP has never been released until now with namespaces (up to version 2.3.0), so this is definitely weird.
If you accidentially use the development branch of the 3.0 version, I must currently suggest not to use it. It barely got any significant commits in the last year, and should be considered work in progress (very slow progress unfortunately).

libphonenumber php cannot find base class Locale

I am using the php implmentation of libphonenumber from
giggsey/libphonenumber-for-php
I need it to just do a basic sense and validity check on a phone number entered by a users.
I have taken the whole src/libphonenumber directory and its sub directories and copied it into my project. I do not use Composer and, as this is only one element of my project, am hoping not to have to climb that particular learning curve just now. I put the following simple autoloader at the start of the script.
namespace libphonenumber;
spl_autoload_register('libphonenumber\myAutoloader');
function myAutoloader($className)
{
$path = 'libphonenumber/';
$parts=explode('\\',$className,2);
$filename=str_replace('\\','/',$parts[1]);
include $path.$filename.'.php';
}
The auto loader works fine except for when it comes to include the file geocoding/Locale.php which includes a single class 'Locale' as follows;
class Locale extends \Locale
The error I get is
PHP Fatal error: Class 'Locale' not found in /var/www/luthor/php/libphonenumber/geocoding/Locale.php on line 6
I am pretty new to namespaces and oo in php. I understand that Locale is a php class. My question is Why is it not found
Found the answer. The use of Locale requires the international extension for php to be installed see the following question
Fatal error: Class 'Locale' not found with ZF2 beta5 skeleton application

Cannot use X as Y because the name is already in use, even though it's not

I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.
A\Library\Session.php:
namespace A\Library;
class Session { ... }
My\Application\Session.php:
namespace My\Application;
class Session { ... }
My\Application\Facebook.php:
namespace My\Application;
use A\Library\Session;
class Facebook { ... }
When I try to run the application, I get the following error:
Cannot use A\Library\Session as Session because the name is already in use in My\Application\Facebook.php
Even though it's not, at least not in this file. The Facebook.php file declares only the Facebook class, and imports exactly one Session class, the A\Library one.
The only problem I can see is that another Session class exists in the same namespace as the Facebook class, but as it was never imported in the Facebook.php file, I thought it did not matter at all.
Am I wrong (in that case please point to the relevant documentation), or is this a bug?
There is a bug confirmed in PHP that may affect the behavior you see. It is supposed to fatal error, but with opcache enabled, it may still execute flawlessly.
https://bugs.php.net/bug.php?id=66773
If it still concerns you, please vote for the bug.
No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing
use A\Library\Session;
is the same as:
use A\Library\Session as Session;
So try using something like:
use A\Library\Session as AnotherSessionClassName;
The only problem I can see is that another Session class exists in the
same namespace as the Facebook class, but as it was never imported in
the Facebook.php file, I thought it did not matter at all.
Yes, it does matter. This is why you don't need to "import" classes from the same namespace. If you have conflicting names from different namespaces, you need to alias the class.
namespace My\Application;
use A\Library\Session as ASession; // choose a proper alias name here
class Facebook { ... }
I've read the thread about the issue, but I tested on many PHP versions (php 5.5, 5.6, 7.*, x32, x64, vc11, vc14, vc5). I'm using Laravel with Laragon. But, when I build up the server with php artisan serve (and open the server at http://localhost:8000) I have the problem of "the namespace that some Class was already used" and stuff.
I tested with and without opcache extension and nothing works, then I tested the virtual domain that Laragon provides and... voila, the error just disappeared and now I can work OK. I don't know what was happening, my namespaces were OK, I had an alias but the same code works in many machine without problem (AWS, local, prod, dev, etc) but only in my machine I had the problem just as I described it.
So, if someone is working with Laravel (5.1) and is having this issue, try the virtual host of Laragon.

PHP Class resolve issue for classes in the same directory when running PHPUnit test cases

I installed PHPUnit and my Test class looks like this:
require_once 'PHPUnit/Framework/TestCase.php';
class Test extends PHPUnit_Framework_TestCase {...}
When I execute the PHP script in Eclipse, I get the following error:
Fatal error: Class 'PHPUnit_Framework_Assert' not found in .../PEAR/PHPUnit/Framework/TestCase.php on line 99
So I created a general PHP classloading test:
A.php and B.php in the same directory
A.php:
class AA {}
B.php:
class BB extends AA {}
new BB();
When executing the PHP script B.php I get the same error:
Fatal error: Class 'AA' not found in .../B.php on line 2
There must be an option for PHP to be able to resolve these classes otherwise PHPUnit could not work. Any ideas?
Thank you.
You should not be loading / require
require_once 'PHPUnit/Framework/TestCase.php';
in your tests at all. The normal phpunit runner should be able to figure that out.
Usually IDEs should care about setting phpunit up properly (or invoking it properly) but if that doesn't work out requiring
require_once 'PHPUnit/Autoload.php';
That should do the trick then as this is whats needed to make PHPUnit working
I ran into this issue when integrating with NetBeans. The solution for me was to load a bootstrap.php file, which would include all necessary dependencies while leaving my class files untouched.
Oops: just realized you're using Eclipse. It should be pretty similar. The problem is likely that your include script is relative to Eclipse's working directory (or some directory other than where you application normally runs). But that's a stab in the dark without being too familiar with Eclipse myself...
In case PHPUnit 6.x is used, then PHPUnit_Framework_Assert class has been removed. You should use namespaces instead, or downgrade to ~4.5.
So replace PHPUnit_Framework_Assert with \PHPUnit\Framework\Assert, ot use statement like:
use PHPUnit\Framework\Assert;
And use Assert directly, e.g. Assert::assertNotEmpty(...);.
Source: Class 'PHPUnit_Framework_Assert' not found (Behat\Testwork\Call\Exception\FatalThrowableError #2585

Categories