I am trying to load a custom exception class that I created according to the instructions here:
http://codeigniter.com/user_guide/general/core_classes.html
MY_Exceptions.php is stored at application/core/
Somehow, when I try loading it, I keep getting this error:
Fatal error: Class 'MY_Exceptions' not found in C:\xampp\htdocs\xampp\facebook\application\models\campaign_model.php on line 29
Based on the instructions, it doesn't say I have to autoload the class or anything. What am I doing wrong?
MY_Exception.php is stored at application/core/
Name the class and file MY_Exceptions with an s
You do not need to autoload or manually load anything in the core directory, nor should you. They are required classes for CI to run that are automatically loaded.
For creating core classes, use this documentation instead: http://codeigniter.com/user_guide/general/core_classes.html
Keep in mind that when calling the class you will use the original class name. Let's say you have created MY_Input. Example:
$this->input->post(); // Do this
$this->my_input->post(); // Don't do this
To understand the why and how, see system/core/Common.php function load_class.
Looking at the docs now, I agree that this should probably be highlighted.
It's trying to find MY_ExceptionS.php, not MY_Exception.php.
Related
The following code is included by another file. My\Engine\Control is defined much earlier and extended all throughout my site with no issues. However in this one file I get the the error:
Fatal error: Class 'My\Engine\Control' not found in
/mnt/web/~/classes.php on line 6
<?php
namespace My\Engine;
// class Control {}
class RequiresAccount extends Control {
public function permissions() {
}
}
Yet when I try to put a dummy Control class in (uncomment the commented part) I get a different error.
Fatal error: Cannot declare class My\Engine\Control, because the name
is already in use in /mnt/web/~/Control.class.php on line 47
It seems impossible and I just can't figure it out. I write code like this all the time and just this one time...
All other files that require extending \My\Engine\Control function perfectly.
It seems that your using some custom autoload logic and some kind of optimizer which puts all the classes together into one file.
I would suggest sticking with PSR-4 standard & Composer library to support and maintain it.
This way your files will be organized and composer will take care of properly handling loading the files and optimizing the loading process for production. All you need to do is to include ./vendor/autoload.php file into your project and define the auto-loading strategy in composer.json file.
Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich
try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();
try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)
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 make use of this Helper class, which was written for earlier version of cake.. It seems that it makes use of the vendor() method to use a third party class, but now that method is deprecated. I am trying to use the following in the helper class to include the third party class called eq_pie.class.php:
App::import('Vendor', 'eq_pie' );
I placed the third party class in the app/vendor folder(which im not sure if its correct)
The helper class makes use of it like so
//vendor('class_eq_pie'); Now commented out..
$this->eq_pie = new eq_pie; //generates error
I get this error: Error: Class 'eq_pie' not found , which means that the third party class is not being included.
What am I missing here? Thanks in advance
Ok, Fellow cakePHP new users... make sure to follow your version's of cake conventions.. the problem was that my current version(2x) expects class names and their respective files to be camelCased rather than underscored..
Fromeq_pie, I changed the class name to EqPieClass, and file toEqPieClass.php. Reference doc here : Class Conventions if you run into trouble. Best of luck
I have a class Called MyClass. It lives in app/Lib/MyDir/MyClass. I'd like to use App:uses() to load it, but can't get it to work.
In CakePHP 1.3 I would load it via:
App::import('Lib', 'MyDir/MyClass'); //this still works in CakePHP 2.1
In CakePHP 2.1 I'm trying to do:
App::uses('MyClass', 'Lib/MyDir');
When I try to 'new' up MyClass I get Class 'MyClass' not found.
Is it not possible to use App::uses on custom Lib classes? I can't continue to use App::import() because if 'App::import('Lib', 'MyDir/MyClass');' appears 2x in the code path I get a 'Cannot redeclare class' error in lib/Cake/Core/App.php on line 531
what am I doing wrong?
Edit: so if I do App::uses('MyClass', 'MyDir'); it works. Not sure if thats how its supposed to behaive, but reporting bug.
did you try
App::uses('MyClass', 'MyDir');
? since "Lib" itself can be seen as a base directory
PS: you are even supposed to group everything inside Lib in subfolders (packages) similar to the core.