2

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.

2
  • yeah, but you're in a namespace (imagine it being a folder), so ServiceDetails is in the "current folder" (App\Http\Controllers). Try using new \ServiceDetails, or the full namespace of that class if it has one Commented Jun 11, 2015 at 13:06
  • @DamienPirsy , Thanx buddy, it works when i used new \ServiceDetails but its a huge project so in a controller lots of files include , and many time object are being created, is any other way to end the scope of namespace, so that we can create object normally? Commented Jun 11, 2015 at 13:21

2 Answers 2

3

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 is namespace ...; at the beginning of the file (after <?php). If not you can simple add in your Lists file after:

use App\Http\Controllers\Controller;

the following line:

use ServiceDetails

and if it is, you should copy that namespace and add the following line:

use namespaceyoucopied\ServiceDetails;

of course in namespaceyoucopied place you need to put the correct copied namespace so it could look like this:

use A\B\C\ServiceDetails;

You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual

Sign up to request clarification or add additional context in comments.

Comments

2

You just need to add a use statement for that class so the class in the current file can "see" it.

<?php
namespace App\Http\Controllers;

$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");

use App\Http\Controllers\Controller;
use Namespace\To\ServiceDetails;

class Lists extends Controller
{
    public function show()
    {
        $objServiceDetails= new ServiceDetails;
        .........
        ........
     }
 }

However, if you are using Laravel and doing this, then you are not using the autoloading feature to its fullest. I recommend you put this file in a namespaced directory in your application and have it follow PSR-4. Then Laravel will load this for you, and it will keep your class file looking clean.

Put the file in a path like the following: /path/to/projectRoot/app/Lib/ServiceDetails.php. Then make the file look like below so it follows PSR-4:

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Lib\ServiceDetails;

class Lists extends Controller
{
    public function show()
    {
        $objServiceDetails= new ServiceDetails;
        .........
        ........
     }
 }

1 Comment

Thanx i think it's standard but actually its a huge app so there is lots of file being include and many objects are being created, so its tough to change every include() to namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.