I am new in laravel and importing a existing php site. I created a controller named "List" then i need to create a object of a class, coded in a file which is been include by include_once() as shown,
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
But i am getting an error like
Fatal error: Class 'App\Http\Controllers\ServiceDetails' not found
I dont have much idea of namespace "use" and "as". May be that's why i am not able to solve this problem. When creating a new object it is searching class in namespace location only, but it should also look in included files, i think.
If there's a namespace in current file, PHP will try to find class in current namespace and if it won't find it, you'll get fatal error. You should open
ServiceDetails.class.php
class to verify if there isnamespace ...;
at the beginning of the file (after<?php
). If not you can simple add in yourLists
file after:the following line:
and if it is, you should copy that namespace and add the following line:
of course in
namespaceyoucopied
place you need to put the correct copied namespace so it could look like this:You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual