I am attempting to call a class in a required file, but I am getting the error
PHP Fatal error: Class 'SampleClass' not found in /home2/domain/public_html/website/v2/wp-content/plugins/myplugin.php on line 23
I can tell that the file is required correctly as I have echoed some text in it and it displays. I am using Wordpress - is there anything weird about Wordpress which would prevent this from working?
As soon as I copied the class into the original file (as opposed to including it from somewhere else) it works fine.
Any ideas? Can provide more info if needed, but I'm opposed to copying the code it here because it would be mostly irrelevant.
To use that class you have to prefix the class name with the namespace for example:-
Instead of:
new Codebird;
You would write:
new Codebird\Codebird;
Related
Is there any way to get any sort of error, be it exceptions, error codes or logging, for when the aliasee introduced by use does not exist?
For example, I tried this one (i.e. using all error signalling I know of):
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
use \Foobar as Frob; // \Foobar is actually unknown
print("still here\n");
Together with this line of command in Bash:
display_errors=on php love.php
and get:
still here
as the sole output. I looked into php_errors.log, but nothing is there.
PHP designers, in their much valued sanity, surely have thought of something?
I understand use is just an alias for alias in the world of PHP, yet I don't see why there should not be errors for fully qualified referees.
use statements as such don't really do anything much. All they do is alias a namespaced name to another name of your choice, henceforth allowing you to refer to that shortened alias instead of the full name. That's it. Nothing more happens. The class/function/file isn't actually being loaded at this point nor any other sort of validation is being done. This is because there are many ways to load the actual class that alias refers to, and you may want to do this after the fact later on. Most likely using an autoloader, which will attempt to load the class when it's first actually used.
So: no, there's no way besides trying to use the class.
$frob = new Frob; // Fatal error: class Foobar not found
PHP does not report any error because there is no error in your code.
The use declaration says that Frob is just another name for \Foobar. It is just a declaration, PHP does not try to load \Foobar or check if it exists at that point.
As long as you don't use either Frob or \Foobar in the code, that use declaration has no effect.
But if you try to use it:
$frob = new Frob();
and \Foobar is not a class then be sure PHP will trigger a fatal error and stop the execution of the script.
The use declaration works like the symbolic links in the file system. You can create the file a that is a symbolic link to b and as long as you don't try to read from a (or b), nobody cares if b exists or not.
I made a helper class which have lots of small functions that will help me to create my content, but when I try to include it in my code the PHP shows an error saying that my class doesn't exist.
I just use require_once('../general.php'); but it gives me a "failed to open stream" error.
Just add the class to application/classes and use as normal. I've used some kind of Util.php class with some static functions like that.
Oh and don't bother with loading it manually, autoloader should deal with it just fine.
Edit:
Make sure that your class starts with a capital letter (General.php) and call it just General in your code.
You just need to call it just by name like if you have Function.php
You can call it through (Function) no need to add php extension.
you should use General not General.php
I am trying to use Michelf's PHP implementation of Markdown.
I'm including his Markdown.php script with include() at the beginning of my main script, but when trying to use the class (be it for creating a new object or using a method directly), I get this:
Fatal error: Class 'Markdown' not found in [my main script]
The class Markdown is cleary defined in included Markdown.php however.
I've checked, of course, that the include works. I've tried placing Markdown.php in both my include_path and my main script's path, it doesn't change anything.
I am at a loss.
Judging by the source file, you most likely forgot to import the class before using it:
use Michelf\Markdown;
// ...
$md = new Markdown();
Alternatively, you could use the canonical name:
$md = new \Michelf\Markdown();
I have the following error
Fatal error: Cannot redeclare class Database in /home/content/63/8026363/html/include/user_database.php on line 5
That links to the line
class Database
But I have not redeclared this anywhere I can see.
I've checked all the include files and still can't see it.
I have now changed the name to user_database1.php which is DEFINITELY only included once in my WHOLE system and I am still getting the same message!
This only occurs in my root/admin directory.
When I moved the file it occurs into the root directory and updated the include files from ../file.php to just file.php, it worked perfectly.
I can't understand why having the file.php in the /admin directory and using ../ to include files isn't working!
Can anyone offer any experience of this? Or a potential fix.
I'll provide some code from the top of the file in question..
<?php
include("../include/session.php");
include("../include/admin_database.php");
Clearly this is the problem but I can't understand why!
Hope someone can help !
(question has been updated significantly since a lot of the answers below were submitted)
If you included the file two times, theen it gets re-declared. Use include_once() instead to prevent that easily.
If you are unsure where that class was originally declared, you can make use of the Reflection API to get the name of the file and the line of code:
$class = 'Database';
if(class_exists($class))
{
$oRefl = new ReflectionClass($class);
$message = sprintf('Class %s already defined in %s on line %s.', $class, $oRefl->getFileName, $oRefl->getStartLine);
throw new DomainException($message);
}
Place that before the line where you define the class Database to find out which file was originally defining the class.
Check if php already defines a file called Database.php.
Maybe you are using some framework or library which does.
You probably have included your database class more than once, a quick fix for this is to add something like this to the top of your class:
if(!class_exists('Database')){
class Database{
// so on
}
This ensures that your class is only defined once.
I have a class file: class_xx.php. And then a function file: function_xxx.php
In my function_xxx.php:
require_once('class_xx.php')
... // after few next lines
$object = new class_xx1($arg1, $arg2);
But it gives me:
Fatal error: Class 'class_xx1' not found in "some_path" on line "1XX3"
[sorry I can't exposed the codes yet], any idea why I included the file > require_once with no error, but it gives me "Class not found error"??
Seemingly, the class_xx.php does not correctly declare the class_xx1 class. Review your code and watch for typos. Put some sort of debug line like echo "hello; in the required file if you want to be sure that it is being included correctly.
Chances are you misspelled the class declaration or something to that avail. You will what to double-check that your spell it the exact same way, with the same casing.
If you are developing on a secondary sever, you might have not transferred the completed class_xxx.php file over and just a blank file, in which case PHP would be including a blank file.
By the way, you forgot a semi-colon after the require_once