I'm trying to get working use.
My file is in /application/controllers/indexController.php
use application\models\Database;
class IndexController extends Controller {
public function indexAction() {
$db = new Database();
$this->view->render('index','template');
}
}
My Database class is in /application/models/Database.php so i wrote use application\models\Database
But it tells
Fatal error: Class 'application\models\Database' not found in
Z:\home\localhost\www\application\controllers\IndexController.php on
line 7
How i can get it worked?
use is not magical.
You need an autoloader to tell it where to find a certain namespace.
So right now your code is looking for a class called application\models\Database in the same directory as your currently executing file.
I think what you meant to do is:
include 'application\models\Database.php';
Related
I'm trying to include a class in PHP and to use the methods from this class in another file. My code looks like that (small reproducable example). By the way: I'm working on a WordPress environment.
Main file:
include 'class-file.php';
$cars = new Cars();
var_dump($cars->getCars());
File class-file.php:
class Cars {
public function getCars() {
return "No Cars";
}
}
I get the following error:
Fatal error: Uncaught Error: Call to undefined method Cars::load() in /..../wp-content/themes/builder/includes/elements.php
Seems that my class instance is connected with another class from the theme. But why? Can I disconnect the class from any others? If yes, how?
Maybe the theme doesn't use namespaces, which makes all its classes discoverable from the global namespace, and by defining your own Cars class, you override the theme's class.
Try to define your namespace, and check if the conflict is gone.
Main file:
include 'class-file.php';
$cars = new \mycode\Cars();
var_dump($cars->getCars());
File class-file.php:
namespace mycode;
class Cars {
public function getCars() {
return "No Cars";
}
}
I am using Lithium Framework. I am trying to call class into a controller, that class is placed inside libraries folder.... But it is showing this error
Fatal error: Class 'app\libraries\Test' not found in /home/ali.mehdi/AvonTPH/app/controllers/SessionsagentController.php on line 34
In libraries folder I created Test.php having following code.
<?php
namespace app\libraries;
class Test{
public static function getTest(){
return "Hi";
}
}
Also Inside my controller.. I used following using statements:
use app\libraries\Test;
But Why Test Class not found... Am I missing something? Any Help would be appreciated.
As #Matei Mihai has commented, in your app\config\bootstrap\libraries.php add the line Libraries::add('.'), though I'd advise against this (see below).
Be aware that the best practice is to put your classes into a package and not drop them in the libraries folder.
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.
Hello guys i created Entity folder inside application/model/. In Entity folder i created file Mj_user.php and give class name as same Mj_user.
but when i try to access that class using
$User= new Entity\Mj_user;
it's giving me error
Fatal error: Class 'Entity\Mj_user' not found in C:\xampp\htdocs\project\application\controllers\user.php on line 15
what should i do Please help me..but when i remove Mj_ then put only file name as User.php. Its working properly..Please help me
When creating a new class you have to be careful, the name of the class must be:
class Mj_user extends CI_Model {
function __contruct()
{
partent::construct();
}
}
The file must be mj_user.php and for using this model you must load it before start using it
it can be preloaded for all in the configuration or you can load it whenever you need it like this
$this->load->view('mj_user');
And in the error seems that is looking for a file called user.php and should be mj_user.php
Im trying to figure out how namespaces works in PHP, but havent really been lucky
Hope somebody could tell me what Im doing wrong here :)
code
require_once 'Vatcode.php';
$Vatcode = new \resource\Vatcode();
Vatcode.php
namespace resource;
require_once Ini::get('path/class').'/Resource.php';
class Vatcode extends Resource {
public function __construct(){
echo 'works!';
}
}
Rescource.php
namespace resource;
class Resource {
}
error
Fatal error: Class 'resource\Ini' not found in Vatcode.php
it's just a problem of namespace.
Your class Vatcode is in namesapce ressource. If, in the file of VatCode declaration you use nameofclas::... or new nameofclass() it will try to get the class in namespace ressource.
If you want to use the class Ini inside your document you have two solutions :
first give the full qualified name :
require \namespace\of\ini\Ini::get('path/class').'/Resource.php';
second using the "use" keyworld before using the get method :
use \namespace\of\ini\Ini;
require_once Ini::get('path/class').'/Resource.php';
In any case, if Ini is in "no namespace" (global namespace is the accurate word) you just has to use the solutions I gave you but only with \Ini instead of \namespace\of\ini\Ini