I am dealing with an old PHP class called Config in a Config.class.php file.
Recently, I have implemented namespaces:
<?php
namespace Tfr\Partners\Smartfocus;
use Tfr\BaseApp;
class Config
{
...
} else {
$isProd = BaseApp::isProd();
}
...
When I am running the code, I now get the following error message:
Fatal error: Class 'Tfr\BaseApp' not found in
<some_path>/Partners/Smartfocus/Config.class.php on line 45
but the BaseClass is there in a file called <some_path>/BaseApp.class.php. However, it does not have a namespace itself.
Unfortunately, it is used in many places in a large code base, and adding a namespace to BaseClass seems very risky.
How can I 'import' BaseClass in Config safely and get rid of my error?
Related
In the same direcory I have:
html/app
Plugin.php
PluginContract.php
GetUid.php
On GetUid.php i have this code:
<?php
namespace App;
include_once "Plugin.php";
include_once "PluginContract.php";
use Carbon\Carbon;
use TeamSpeak3\Ts3Exception;
class GetUid extends Plugin implements PluginContract
{
public function isTriggered()
{
...
}
}
And this is PluginContract.php:
<?php
namespace App;
interface PluginContract
{
public function isTriggered();
}
It seems all ok, but I got this error:
PHP Fatal error: Interface 'App\PluginContract' not found in /var/www/html/app/GetUid.php on line 11
The weird thing is that it can load Plugin.php without problem but it gets this error for PluginContract.php which is in the same folder.
What I'm doing wrong?
What I was tring to do is add a web interface to a PHP application, I though that I don't need the autoload because it automatically start with the server, but I discovered that adding again the autoload to my index.php I resolved the problem.
I am new to PHP and would appreciate some help with namespaces.
I have a class and it's declared as:
namespace P3;
class CardstreamCodingStandard_Sniffs_Classes_ClassDeclarationSniff {
}
Now i want to implement an interface called CodeSniffer_Sniff. So i amended the class declaration as:
namespace P3;
class CardstreamCodingStandard_Sniffs_Classes_ClassDeclarationSniff extends CodeSniffer_Sniff {
}
But when I run the code I get
Fatal error: Interface 'P3\CodeSniffer_Sniff' not found in /root/qa/CardstreamCodingStandard/Sniffs/Classes/ClassDeclarationSniff.php
Please can someone explain what is going on? and what the correct syntax should be?
Thanks
Ok some more details
I have inherited this code that implements a sniff for phpcs
My class is declared as
namespace CardstreamCodingStandard\Sniffs\Classes;
class CardstreamCodingStandard_Sniffs_Classes_ClassDeclarationSniff implements PHP_CodeSniffer_Sniff {
}
when I run phpcs with this sniff then I get the error
PHP Fatal error: Interface 'CardstreamCodingStandard\Sniffs\Classes\PHP_CodeSniffer_Sniff' not found in /root/qa/CardstreamCodingStandard/Sniffs/Classes/ClassDeclarationSniff.php on line 23
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpcs:0
PHP 2. PHP_CodeSniffer_CLI->runphpcs() /usr/bin/phpcs:25
PHP 3. PHP_CodeSniffer_CLI->process() /usr/share/pear/PHP/CodeSniffer/CLI.php:113
PHP 4. PHP_CodeSniffer->initStandard() /usr/share/pear/PHP/CodeSniffer/CLI.php:956
PHP 5. PHP_CodeSniffer->registerSniffs() /usr/share/pear/PHP/CodeSniffer.php:594
PHP 6. include_once() /usr/share/pear/PHP/CodeSniffer.php:1409
I hope this makes things clearer
Some details on how to use namespaces can be found at the php manual.
In your case it seems you are using the same namespace.
To correctly use an interface, you have to implement it into your class.
The correct use would be the following:
Class Apple implements Fruit { ... }
More on interfaces and how to use them can be found here.
You are using the extends keyword which is used for creating child classes of a parent class.
The php manual on parent and child classes (called inheritance) has been linked here
you can follow this role :
file1:
<?php
namespace foo;
class Cat {
public function says(){
echo 'meoow';
}
}
?>
file2:
<?php
include 'file1.php';
use foo;
class bar extends foo{
//......
}
more details: http://php.net/manual/en/language.namespaces.php
You have to implement an interface, not extend. Try this:
class CardstreamCodingStandard_Sniffs_Classes_ClassDeclarationSniff implements CodeSniffer_Sniff {
}
I want to create and use a custom exception class in my CakePhp Application.
So I created a DuplicateConfigurationException.php with the following class skeleton:
<?php
namespace Cake\Exception;
class DuplicateConfigurationException extends Exception{
} ?>
I a controller, where I wish to raise the Exception, I added
use Cake\Exception\DuplicateConfigurationException;
and within a function I call
throw new DuplicateConfigurationException();
Following suggestions throughout the interwebs, I have tried to place the php file in the following locations, but neither of them seems to work:
src/Exception
src/Exceptions
src/Lib
src/Lib/Error
src/Lib/Error/Exceptions
I always get an error:
Error: Class 'Cake\Exception\DuplicateConfigurationException' not found
File /host/var/www/src/Controller/StructuresController.php
Line: 246
What else do I need to do to make Cake recognize my custom exception?
I'm well aware of Loading custom class in CakePHP3, but since this exception is not a separate library I would rather not place it within vendor?
A bit late but I think it might be useful for other users with the same question to have some further explanations.
In fact, with your solution, you rely on native PHP SPL Exception class located in global namespace.
To use Cake's basic Exception class, you missed to add
use Cake\Core\Exception\Exception;
in src/Exceptions/DuplicateConfigurationException.php for loading Cake Exception class constructor. See Cake's book
Your code is working because Cake is handling SPL exceptions the same way than its own Exception class. If you've wanted to go further with a custom handler for instance, it may have broken logic.
Note that class IniPermissionsException extends \Cake\Core\Exception\Exception {}; is also working. In this case, you must prepend \ as the root namespace when calling a class in an extends statement because you need to provide full namespace path.
To swim like a dolphin in Cake's namespaces, just go to API reference.
Full updated code for src/Exceptions/DuplicateConfigurationException.php :
<?php
namespace App\Exceptions;
use Cake\Core\Exception\Exception;
class DuplicateConfigurationException extends Exception {}
?>
Ok, after some fiddling I managed to get it working:
in src/Exceptions/DuplicateConfigurationException.php
<?php
namespace App\Exceptions;
class DuplicateConfigurationException extends \Exception{
} ?>
in the controller:
use App\Exceptions\DuplicateConfigurationException;
...
function somefunction(){
throw new DuplicateConfigurationException();
}
Apparently the namespace should be App\<Folder> and App\<Folder>\<Classname>, respectively.
And I had to prepend Exception with a backslash, since it is used in a namespaced context: http://www.php.net/manual/en/language.namespaces.global.php
Still, I'm not sure where the namespace conventions for CakePhp 3 are documented.
I have two classes in the same folder in own files. But when I am trying to extends one to another it is giving namespace and class not found error.
Info: It is the first time I am extending class using namespace. Also nested namespace is new to me. DB\CRUD So may be I am doing
completely wrong with namespace.
Error message:
Fatal error: Class 'DB\AT_Database' not found in /var/www/...
DB class
File: AT_Database.php
namespace DB;
class AT_Database
{
...
}
CRUD class
File: AT_CRUD.php
namespace DB\CRUD;
use DB\AT_Database;
class AT_CRUD extends AT_Database
{
public function __construct()
{
}
}
This may be silly mistake or may be I have overlooked it (which I should not as a programmer) and that is loading sequence of the class.
May be it's not worth to have as an answer but just adding so by chance in future it can help to someone who make such mistake.
As I mentioned in one of my comment, I am using glob to auto load all class files to include.
foreach ( glob( $this->classes_dir . "/*.php" ) as $class ) {
include_once $class;
}
Now my file names are AT_CRUD.php and AT_Database.php. Here I realized that php loads files in alphabetical order. So when I extends AT_Database class into AT_CRUD its never found.
This is just because php loads AT_CRUD first than AT_Database so either I have to instantiate the class into or to use something like dependancy injection as #prehfeldt mention in his comment.
I use symfony 2.4.0. I want to use my custom class as discussed here: Autoloading a class in Symfony 2.1. I have created subfolder in src:
namespace Yur;
class MyClass {
public go() {
var_dump('hello!! 32');
}
}
In my controller, I made this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Yur\MyClass;
class WelcomeController extends Controller
{
public function indexAction()
{
$my = new MyClass();
$my->go();
die();
...
but it makes an exception:
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
After I have got this exception, I decided consciously to make syntax error exception in my class to see if it loaded. I changed class MyClass ... to class4 MyClass ..., but doesnt got asyntax error` exception. And I decided, that my class is not loaded.
Is anyone knows why? And what I must to do to resolve?
A few things. First, in your code sample above, you have
public go() {
var_dump('hello!! 32');
}
which should be
public function go() {
var_dump('hello!! 32');
}
The former raises a parser error in PHP. and probably isn't what you want.
Second, the error
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
is the error Symfony uses when it attempt to autoload a class, but can't find the file. This usually means your file is named incorrectly, or in the wrong folder. I'd tripped check that you have a file in the directory you think you do.
$ ls src/Yur/MyClass.php
You can also add some debugging to the composer autoload code to see what path it's cooking up for your custom class
#File: vendor/composer/ClassLoader.php
public function findFile($class)
{
//...
$classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';
//start your debugging code
if($class == 'Yur\MyClass')
{
//dump the generated path
var_dump($classPath);
//dump the default include paths
var_dump($this->fallbackDirs);
}
//...
}