I am trying to upgrade the code of a new project I am working on to comply with PSR-0.
I am using and SPL loader class, However I may be doing something wrong, I just can't spot what the issue is.
I keep getting the following error:
Fatal error: Class 'widezike\General' not found in /nfs/c03/h04/mnt/169128/domains/widezike.com/html/beta/lib/functions.php on line 14
This is my folder structure:
index.php
-lib
config.php
init.php
spl-class-loader.php
functions.php
-widezike
-General.php
This is my functions file where it begins anything to do with the server side code:
<?php
include 'init.php';
include 'config.php';
include 'spl-class-loader.php';
$loader = new SplClassLoader('General', 'lib/widezike');
$loader->register();
use widezike\General;
//Run the output buffer
General::ob();
So this is my code for now, but I can't seem to find what's causing the fatal error...
Thanks in advance
I'm taking a bit of a guess here but I think the issue is in the constructor..
$loader = new SplClassLoader('General', 'lib/widezike');
In the code you linked to they are the namespace and the include path.
Play around with those until it works is all I can suggest.
You might try:
$loader = new SplClassLoader(null, 'lib');
Or
$load = new SplClassLoad('General', 'lib');
On the other hand I personally just use a very simple spl_autoload_register function to do my class loading for me..
spl_autoload_register(function($class){
$filename = str_replace('\\', '/', $class) . '.php';
require($filename);
});
$object = new phutureproof\common\whatever();
I would then have a file /phutureproof/common/whatever.php with the contents:
<?php
namespace phutureproof\common;
class whatever
{
}
I realised that the issue was that my name spaces were wrong, so if you have the same issue just check and make sure that your namespaces are correct!
Related
I saw answers to similar questions but none of them worked for me
this is my implementation of the function:
spl_autoload_register(function($class){
require_once 'classes/'. $class .'.php';
});
so when i say
$user = new User();
it works fine as i have a User.php file inside the classes folder with the class defenition
but when I use a native PHP class like
$date = new Date();
I get the following error:
Fatal error: require_once() [function.require]: Failed opening required 'classes/Date.php'
I tried surrouding the require_once in a try block but it did work, i still get the same error
Any ideas?
You can use file_exists to check if you have a class in your classes directory named with $class. If not, you don't require it.
If it is a PHP native class, it will work, otherwise it will throw you an error.
spl_autoload_register(function($class){
if (file_exists('classes/'. $class .'.php'))
require_once 'classes/'. $class .'.php';
});
I am new to namespaces and I guess autoloading in the method of SplClassLoader. I've tried search for many tutorials but not having much luck. Perhaps you guys can help me get this going?
Directory Structure
- Oram
- Lib
- Classes
Test.php
- index.php
- SplClassLoader.php
Test.php
<?php
namespace Oram\Lib\Classes;
class Test
{
function __construct()
{
echo "Test Class loaded";
}
}
index.php
<?php
require_once('SplClassLoader.php');
$loader = new SplClassLoader('Lib', 'Oram\Lib');
$loader->register();
use Oram\Lib\Classes\Test;
$test = new Test();
This is all inside localhost/website/ too btw as I am running it on WAMP.
Fatal error: Class 'Oram\Lib\Classes\Test' not found in C:\Program Files\wamp\www\website\index.php on line 10
Any advice or if someone could point me to some reading resources to get my head around this would be great.
Thank you
Edit:
I have it!
I think you have to change the backslash (\) to a slash (/).
<?php
require_once('SplClassLoader.php');
$loader = new SplClassLoader('Lib', 'Oram/Lib');
$loader->register();
use Oram\Lib\Classes\Test;
$test = new Test();
?>
Take a look on [this][1].
Try that. I have added a \ before your namespace. That could be the answer:
$loader = new SplClassLoader('Lib', '\Oram\Lib');
Let's say I've got two files class.php and page.php
class.php
<?php
class IUarts {
function __construct() {
$this->data = get_data('mydata');
}
}
?>
That's a very rudamentary example, but let's say I want to use:
$vars = new IUarts();
print($vars->data);
in my page.php file; how do I go about doing that? If I do include(LIB.'/class.php'); it yells at me and gives me Fatal error: Cannot redeclare class IUarts in /dir/class.php on line 4
You can use include/include_once or require/require_once
require_once('class.php');
Alternatively, use autoloading
by adding to page.php
<?php
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
$vars = new IUarts();
print($vars->data);
?>
It also works adding that __autoload function in a lib that you include on every file like utils.php.
There is also this post that has a nice and different approach.
Efficient PHP auto-loading and naming strategies
In this case, it appears that you've already included the file somewhere. But for class files, you should really "include" them using require_once to avoid that sort of thing; it won't include the file if it already has been. (And you should usually use require[_once], not include[_once], the difference being that require will cause a fatal error if the file doesn't exist, instead of just issuing a warning.)
Use include_once instead.
This error means that you have already included this file.
include_once(LIB.'/class.php');
use
require_once(__DIR__.'/_path/_of/_filename.php');
This will also help in importing files in from different folders.
Try extends method to inherit the classes in that file and reuse the functions
Use include("class.classname.php");
And class should use <?php //code ?> not <? //code ?>
I'm trying to include a custom path to a certain file using php's set_include_path. Here's the code:
file.php
<?php
$path = 'classes/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
$obj = new MyClass();
$obj->methodCall();
?>
Here's my root directory structure
www
|_webapp
|_classes
|_MyClass.php
|_nbproject
|_file.php
All I get when I execute the script is this error message: Fatal error: Class 'MyClass' not found in C:\wamp\www\webapp\file.php. I have tried including the file using require and it works but I have hit a wall with set_include_path. Does anybody know what I can do about this?
Thanks
You're adding a relative path to the global include path. Is that what you intended?
You are possibly not including the file where class MyClass is defined. set_include_path() allows to omit the path in the require_once statement, not to omit the statement itself.
I have the impression that the tool you have in mind is class autoloader.
You choose include method like this format include 'path/filename.php'
I'm currently doing this but it doesn't look right to me:
spl_autoload_register(function ($class_name){
$class_name = str_replace('MyNameSpace\\', '', $class_name . '.php');
require $class_name;
});
Please advise.
The namespace is passed because it has to be. How else would the autoloader function know the difference between Foo\Bar and Baz\Bar? :-)
Your method looks okay if you're absolutely sure that you won't ever need to load classes with the same names as those found in MyNameSpace. The canonical method to autoloading classes involves using the parts of the namespace as file system structure, so that, for example, foo\bar\Baz can be found at path foo/bar/Baz.php.